简体   繁体   中英

Retrieve value from mysql database json_encode the value in php

I want to retrieve value from mysql database and covert it into json.

  <?php
   $con = mysql_connect("localhost","name","password");
   if (!$con)
   {
       die('Could not connect: ' . mysql_error());
   }
   mysql_select_db("dbname", $con);

   $result = mysql_query("SELECT Inc_number FROM Increment WHERE id=1"); 
   while($row = mysql_fetch_array($result))
   {
  echo $row['Inc_number'];
   echo "<br />";
   }
   mysql_close($con);
   $objJSON['sample'] = $result;
   $objJSON = json_encode($objJSON);
   echo($objJSON);
  ?>

I get the output like this,

4

{"sample":null}

I want 4 instead of null. What am i doing wrong here? Help me please

Thanks,

What you want is $row['Inc_number'] , not $result .

$result = mysql_query("SELECT Inc_number FROM Increment WHERE id=1");
$row = mysql_fetch_array($result);
echo json_encode(array('sample' => $row['Inc_number']));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM