mysql_query保留返回表中创建的数据类型吗? [英] mysql_query preserve the data type created in the table on return?
本文介绍了mysql_query保留返回表中创建的数据类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在mysql中有一个表:
I have a table in mysql:
CREATE TABLE user (id INT, name VARCHAR(250));
我查询表格:
$result = mysql_query("SELECT id,name FROM user");
我收集结果:
while($row = mysql_fetch_assoc($result) $rv[] = $row;
然后我返回数据:
echo json_encode($rv);
问题在于,即使在json_encode之前,id也会以字符串而不是int的形式返回.如果我强制$ row ['id'] =(int)$ row ['id']; -很好.
The problem is that the id returns as string and not int, even before the json_encode. If i force $row['id'] = (int)$row['id']; - it works fine.
如何强制mysql在$ row中返回正确的数据类型?
How can i force mysql to return the correct data type in the $row?
10倍.
推荐答案
$stmt = $dbh->prepare($sql);
$stmt->bindColumn('int_col', $intVar, PDO::PARAM_INT);
$stmt->bindColumn('string_col', $strVar, PDO::PARAM_STR);
$stmt->execute();
$stmt->fetch(PDO::FETCH_BOUND);
$ intVar将自动转换为int
$intVar will be automatically converted to int
这篇关于mysql_query保留返回表中创建的数据类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文