mysql_query仅返回int/fload数据库类型的字符串类型 [英] mysql_query returns only string type for int/fload db type

查看:88
本文介绍了mysql_query仅返回int/fload数据库类型的字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理MySQL选择结果.我的问题是,即使对于包含整数和浮点数的列,下面的示例代码也仅返回包含所有值为 string 类型的数组.

I am trying to process a MySQL select result. My problem is that the sample code below only returns an Array containing all values as string-type even for columns that contain integers and floats.

$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth)) {
  foreach ($r as $value) echo gettype($value), "\n";
}

如果将$r中的该数组传递给json_encode(..),我将在输出中得到JavaScript字符串值(用引号引起来),而不是未加引号的数字值(我需要).

If I pass that array in $r to json_encode(..) I get JavaScript string values in the output (surrounded by quotes) and not unquoted number values (what i need).

如何查询MySQL数据库并获取包含正确类型的值的行数组?

How can I query the MySQL database and get a row-array containing the values with the correct type?

更新:已删除mysql_fetch_row(..)的第二个参数.最初,我使用mysql_fetch_array(..)接受第二个参数.

Update: Second parameter of mysql_fetch_row(..) removed. Originally I used mysql_fetch_array(..) which accepts the second parameter.

推荐答案

mysql_fetch_*函数均不应根据其列的类型来转换值.对于某些人来说,PHP甚至没有提供匹配的内部数据表示形式(请考虑:BIGINT).

None of the mysql_fetch_* functions are supposed to cast values according to their columns' types. For some, PHP doesn't even provide a matching internal data representation (think: BIGINT).

如果您知道查询结果的数据类型,则需要手动进行强制转换.否则, mysql_field_type() 会提示您将结果转换为哪种类型.

If you know the data-type of a query's result, you'll need to cast them manually. Otherwise mysql_field_type() would give you a hint on what type to convert the results to.

这篇关于mysql_query仅返回int/fload数据库类型的字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆