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.
我正在尝试处理一个MySQL选择结果。我的问题是,下面的示例代码只返回一个数组,该数组将所有值作为字符串类型,甚至对于包含整数和浮点数的列也是如此。
$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth)) {
foreach ($r as $value) echo gettype($value), "\n";
}
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).
如果我将这个数组以$r的形式传递给json_encode(..)我在输出中获得JavaScript字符串值(用引号括起来),而不是没有引号的数字值(我需要的)。
How can I query the MySQL database and get a row-array containing the values with the correct type?
如何查询MySQL数据库并获取包含正确类型值的行数组?
Update: Second parameter of mysql_fetch_row(..)
removed. Originally I used mysql_fetch_array(..)
which accepts the second parameter.
更新:删除mysql_fetch_row(.)的第二个参数。最初我使用mysql_fetch_array(.)来接受第二个参数。
3 个解决方案
#1
4
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_fetch_*函数都不应该根据列的类型转换值。对于某些人来说,PHP甚至不提供匹配的内部数据表示(例如:BIGINT)。
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_field_type()将为您提供将结果转换为何种类型的提示。
#2
0
I suggest folow code:
我建议跟随代码:
<?php
$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth,MYSQL_NUM)) {
foreach ($r as $key => $value)
{
if (is_numeric($value)) {
$r[$key] = (float)$value;
}
}
}
?>
#3
0
In the end I changed my code as follows:
最后我修改了我的代码如下:
$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth)) {
for ($i = 0; $i < count($r) ; $i++)
settype($r[$i],float);
}
It assumes that all selected columns in the result set contain numeric values.
它假定结果集中所选的所有列都包含数值。
#1
4
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_fetch_*函数都不应该根据列的类型转换值。对于某些人来说,PHP甚至不提供匹配的内部数据表示(例如:BIGINT)。
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_field_type()将为您提供将结果转换为何种类型的提示。
#2
0
I suggest folow code:
我建议跟随代码:
<?php
$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth,MYSQL_NUM)) {
foreach ($r as $key => $value)
{
if (is_numeric($value)) {
$r[$key] = (float)$value;
}
}
}
?>
#3
0
In the end I changed my code as follows:
最后我修改了我的代码如下:
$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth)) {
for ($i = 0; $i < count($r) ; $i++)
settype($r[$i],float);
}
It assumes that all selected columns in the result set contain numeric values.
它假定结果集中所选的所有列都包含数值。