JSON 编码 MySQL 结果 [英] JSON encode MySQL results

查看:27
本文介绍了JSON 编码 MySQL 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 json_encode() 函数用于 MySQL 查询结果?我需要遍历行还是可以将其应用于整个结果对象?

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($conn, "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

注意:mysql 从 PHP 5.5.0 开始被弃用,使用 mysqli 扩展代替 http://php.net/manual/en/migration55.deprecated.php.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆