PHP如何将数据库提取的数据转换为JSON格式并展示输出?

更新于
2026-09-23 07:12:43
0阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计144个文字,预计阅读时间需要1分钟。

PHP如何将数据库提取的数据转换为JSON格式并展示输出?

以下是对给定代码的简化

phpheader('Content-Type: application/json; charset=utf8');$results=array();while ($row=mysql_fetch_assoc($result_query)) { $results[]=$row;}if ($results) { echo json_encode($results);} else { echo mysql_error();}

如下所示:

header('content-type:application/json;charset=utf8'); $results = array(); while ($row = mysql_fetch_assoc($result_query)) { $results[] = $row; } if($results){ echo json_encode($results); }else{ echo mysql_error(); }

将查询到的数组存放到一个新的数组中,然后返回json格式(查询部分的语句已经省略)。

以上这篇php将从数据库中获得的数据转换成json格式并输出的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

阅读全文

本文共计144个文字,预计阅读时间需要1分钟。

PHP如何将数据库提取的数据转换为JSON格式并展示输出?

以下是对给定代码的简化

phpheader('Content-Type: application/json; charset=utf8');$results=array();while ($row=mysql_fetch_assoc($result_query)) { $results[]=$row;}if ($results) { echo json_encode($results);} else { echo mysql_error();}

如下所示:

header('content-type:application/json;charset=utf8'); $results = array(); while ($row = mysql_fetch_assoc($result_query)) { $results[] = $row; } if($results){ echo json_encode($results); }else{ echo mysql_error(); }

将查询到的数组存放到一个新的数组中,然后返回json格式(查询部分的语句已经省略)。

以上这篇php将从数据库中获得的数据转换成json格式并输出的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

阅读全文