0

I have the following php code, I expect to outprint the query result starting with [ or { , but I obtain this result "\u0633\u064a\u0627\u0631\u0627\u062a "

<?php
 mysql_connect("localhost", "root", "password") 
or die(mysql_error());
mysql_select_db("db") or die(mysql_error());


$result = mysql_query(" select  part_name from  Services_parts where  part_id=  1 ") 
or die(mysql_error());  
while( $row = mysql_fetch_array( $result ) )
{
echo json_encode($row['part_name']) ;
} 

?>
JEREEF
  • 51
  • 8

1 Answers1

2

If you want an array output as JSON then add your rows to an array and then jsonify the array like this

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());


$result = mysql_query(" select  part_name from  Services_parts where  part_id=  1 ") or die(mysql_error());  

$j_out = array();
while( $row = mysql_fetch_array( $result ) )
{
    $j_out[] = $row['part_name'];
} 
echo json_encode($j_out);
?>

If you want an object that contains an array of results then do this

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());


$result = mysql_query(" select  part_name from  Services_parts where  part_id=  1 ") or die(mysql_error());  

$j_out = new stdClass();

while( $row = mysql_fetch_array( $result ) )
{
    $j_out->part_names[] = $row['part_name'];
} 
echo json_encode($j_out);
?>

Actually as you are only getting a single row from the database you could do this, removing the need for a unnecessary loop and making the returned object easier to deal with in the javascript.

<?php
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());


$result = mysql_query(" select  part_name from  Services_parts where  part_id=  1 ") or die(mysql_error());  

$row = mysql_fetch_array( $result );

$j_out = new stdClass();
$j_out->part_name = $row['part_name'];

echo json_encode($j_out);
?>

Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • in your second code I get Fatal error: Cannot use object of type stdClass as array – JEREEF Dec 22 '15 at 11:51
  • Yep, sorry small typo. I fixed it – RiggsFolly Dec 22 '15 at 11:52
  • Added a cleaner option as you are only returning one row from the database – RiggsFolly Dec 22 '15 at 11:55
  • @RiggsFolly: i want to know the result of this: $row = mysql_fetch_array( $result ); if i not use while loop and direct use this array in json_encode what should be happened? – devpro Dec 22 '15 at 11:56
  • The while loop is only necessary if there will be more than one result row returned by your query. As your query used a criteria of `part_id = 1` I ssume this is a unique key and threfore you will only get one result from this query – RiggsFolly Dec 22 '15 at 12:13
  • @RiggsFolly Could I get the following output {"\u0633\u064a\u0627\u0631\u0627\u062a "} instead of {"part_name":"\u0633\u064a\u0627\u0631\u0627\u062a "} ? – JEREEF Dec 22 '15 at 12:47
  • No. the `{}` denote an object in JSON and you cannot have data in an object without it having a name. And in an array you would get `["0" : "\u0633\u064a\u0627\u0631\u0627\u062a"]` so you would have the array index instead of a property name – RiggsFolly Dec 22 '15 at 12:51
  • But remember when the data gets to javascript and then gets converted from the JSON String to an javascript object or array it is again easy to deal with as its a natice javascript object or array – RiggsFolly Dec 22 '15 at 12:53