1

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!

4
  • 1
    $int_info=intval($info); it looks like you're forcing your value to an int. Commented Feb 28, 2013 at 16:50
  • floatval() is in the question title, but not in the code. Commented Feb 28, 2013 at 16:51
  • 2
    Obligatory: The mysql_* functions will be deprecated in PHP 5.5. It is not recommended for writing new code as it will be removed in the future. Instead, either the MySQLi or PDO and be a better PHP Developer. Commented Feb 28, 2013 at 16:54
  • thanks for your help. actually i started to use php only one week ago, and it was really difficult times for me :) Commented Feb 28, 2013 at 17:16

2 Answers 2

2

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

$int_info = (float)$info[0];
Sign up to request clarification or add additional context in comments.

Comments

2

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; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.