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']);