MySQL整数字段在PHP中以字符串形式返回 [英] MySQL integer field is returned as string in PHP
本文介绍了MySQL整数字段在PHP中以字符串形式返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MySQL数据库中有一个表字段:
I have a table field in a MySQL database:
userid INT(11)
所以我使用此查询将其调用到我的页面:
So I am calling it to my page with this query:
"SELECT userid FROM DB WHERE name='john'"
然后是处理我的结果:
$row=$result->fetch_assoc();
$id=$row['userid'];
如果我这样做:
echo gettype($id);
我收到一个字符串。这不应该是一个整数吗?
推荐答案
当您使用PHP从MySQL数据库中选择数据时,数据类型将总是被转换为字符串。您可以使用以下代码将其转换回整数:
When you select data from a MySQL database using PHP the datatype will always be converted to a string. You can convert it back to an integer using the following code:
$id = (int) $row['userid'];
或者使用函数 intval()
:
$id = intval($row['userid']);
这篇关于MySQL整数字段在PHP中以字符串形式返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文