Begayim Muratalina
Begayim Muratalina

Reputation: 661

floatval returns 1 php error

I have a mysql table with float variables. I want to pass them to mongodb collection, but when i do it, php convert my float variables into string. I've tried to use this code to set variable type as float but it returns me 1 in every field:

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));

i can't find my mistake, any help will be really appreciated! Thanks!

Upvotes: 1

Views: 1268

Answers (2)

str
str

Reputation: 44969

mysql_fetch_row returns an array, not a value. So you have to use:

$int_info = (float)$info[0];

Upvotes: 2

Nick Andriopoulos
Nick Andriopoulos

Reputation: 10643

intval will return only the integer part of a number (or a number in a string). Try casting the value to a float like

$float_value = (float) $sql_number; 

Upvotes: 2

Related Questions