English 中文(简体)
单体/高载体类型
原标题:mysql_query returns only string type for int/fload db type
  • 时间:2012-01-13 15:21:21
  •  标签:
  • php
  • mysql

我正试图处理我的SQL的选定结果。 我的问题是,下面的样本代码只回收包含所有数值的阿雷拉(string-型,甚至包括ger和浮标的栏目。

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

如果我通过<代码>(r至json_encode(.)。 我在产出中拿到 Java本说明价值(用引文衡量),而不是未经引述的数字价值(需要)。

我怎么能够问一下MySQL数据库,并找到一个含有正确类型的价值观的不断记录?

<>Update: 删除了第二个参数:mysql_fetch_row(.)。 最初,我使用的是mysql_fetch_array(,接受第二个参数。

最佳回答

<代码>mysql_fetch_*的任何功能都不应按照各栏的类型来表示数值。 在某些方面,PHP甚至提供相应的内部数据代表(见)。

如果你知道询问结果的数据类型,你就不必人工填写。 页: 1 给你以将结果转化为什么类型。

问题回答

我建议采用低温法:

<?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;
     }
  }
}
?>

最后,我修改了我的法典如下:

$sth = mysql_query($selectstr);
$rows = array();
while($r = mysql_fetch_row($sth)) {
  for ($i = 0; $i < count($r) ; $i++) 
    settype($r[$i],float); 
}

该款假定,计算结果中的所有选定栏目都含有数字数值。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签