简体   繁体   English

如何使用PHP将mysql结果转换为JSON对象

[英]how can I convert mysql result into JSON object using php

I am trying to convert a mysql resource id to JSON object to be sent as a response for ajax call this is the code that I´ve tried to use 我试图将一个mysql资源id转换为JSON对象作为ajax调用的响应发送这是我试图使用的代码

<?php
require_once 'config.php';
$sql="SELECT * FROM `inquiry` WHERE 1";
$res=mysql_query($sql);
if($res)
echo json_encode($res);
?>

and I get this error message 我收到此错误消息

Warning: [json] (php_json_encode) type is unsupported, encoded as null in /home/smartco/public_html/kb/q.php on line 6

null 空值

how can I do it 我该怎么做

Change your code something to 将代码更改为

$res = mysql_query($sql);
while($data = mysql_fetch_array($res)) {
 $output[] = $data;
}
echo json_encode($output);

You're encoding the resource id. 您正在编码资源ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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