32

I want to convert my database query's result array to JSON format in PHP. Here is my code:

$row = mysql_fetch_array($result)

I want to convert $row to JSON format and pass the JSON data to a jQuery plugin.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

64

json_encode is available in php > 5.2.0:

echojson_encode($row);

aksu
  • 5,221
  • 5
  • 24
  • 39
jspcal
  • 50,847
  • 7
  • 72
  • 76
6
$result = mysql_query($query) or die("Data not found."); 
$rows=array(); 
while($r=mysql_fetch_assoc($result))
{ 
$rows[]=$r;
}
header("Content-type:application/json"); 
echo json_encode($rows);
santhosh
  • 77
  • 1
  • 1