JSON编码MySQL结果 [英] JSON encode MySQL results
本文介绍了JSON编码MySQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在MySQL查询结果中使用json_encode()
函数?我需要遍历行还是可以将其应用于整个结果对象?
How do I use the json_encode()
function with MySQL query results? Do I need to iterate through the rows or can I just apply it to the entire results object?
推荐答案
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
函数json_encode
需要PHP> = 5.2和 php-json 软件包-如
The function json_encode
needs PHP >= 5.2 and the php-json package - as mentioned here
注意:从PHP 5.5.0开始不推荐使用mysql
,请改为使用mysqli
扩展名
NOTE: mysql
is deprecated as of PHP 5.5.0, use mysqli
extension instead http://php.net/manual/en/migration55.deprecated.php.
这篇关于JSON编码MySQL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文