将 MySQL 结果数组转换为 JSON

2022-08-30 14:11:56

我想在PHP中将数据库查询的结果数组转换为JSON格式。这是我的代码:

$row = mysql_fetch_array($result)

我想转换为JSON格式并将JSON数据传递给jQuery插件。$row


答案 1

json_encode在 php > 5.2.0 中可用:

echojson_encode($row);


答案 2
$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);

推荐