1

我有一个带有浮点变量的 mysql 表。我想将它们传递给 mongodb 集合,但是当我这样做时,php 将我的浮点变量转换为字符串。我尝试使用此代码将变量类型设置为浮点数,但它在每个字段中返回 1:

foreach( $mytables as $table => $struct ) {
  $sql = mysql_query("SELECT num_auto FROM XL_10331_EXPLOIT_251012 where flightid like '191622'") or die( mysql_error() );
  $count = mysql_num_rows( $sql );
  // If it has content insert all content
  if( $count > 0 ) {
    while($info = mysql_fetch_row($sql)) {
        $int_info=intval($info);
          $mosql[]=($int_info);
  }

   // Starts new collection on mongodb
  $collection = $modb->floatdb;

  $collection->insert(array('num_auto'=>$mosql));

我找不到我的错误,任何帮助将不胜感激!谢谢!

4

2 回答 2

2

intval将仅返回数字的整数部分(或字符串中的数字)。尝试将值转换为浮点数

$float_value = (float) $sql_number; 
于 2013-02-28T16:50:29.413 回答
2

mysql_fetch_row返回一个数组,而不是一个值。所以你必须使用:

$int_info = (float)$info[0];
于 2013-02-28T16:52:29.927 回答