php+mysql将数据库查询结果返回为Json格式,实例代码如下:php+mysql将数据库查询结果返回为Json格式,实例代码如下: ?php // 配置json header("Content-type: application/json;charset=UTF-8"); // 跨域配置 header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Origin: *"); $conn = mysqli_connect('localhost','root','123456
<?php
// 配置json
header("Content-type: application/json;charset=UTF-8");
// 跨域配置
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *");
$conn = mysqli_connect('localhost','root','123456','genbanorg');
mysqli_query($conn,"set character set 'utf8'");
mysqli_query($conn,"set names 'utf8'");
$result=mysqli_query($conn,"select * from prolist");
$jarr = array();
while ($rows=mysqli_fetch_array($result)){
$count=count($rows);//不能在循环语句中,由于每次删除 row数组长度都减小
for($i=0;$i<$count;$i++){
unset($rows[$i]);//删除冗余数据
}
array_push($jarr,$rows);
}
$json = JSON_encode($jarr,JSON_UNESCAPED_UNICODE);
$arr = json_decode($json);
echo $json;
// echo $arr;
// echo $str=json_encode($jarr);//将数组进行json编码
?>
织梦狗教程
本文标题为:php+mysql将数据库查询结果返回为Json格式


基础教程推荐
猜你喜欢
- ThinkPHP5 框架引入 Go AOP,PHP AOP编程项目详解 2023-04-19
- 基于laravel Request的所有方法详解 2023-02-21
- 启用OPCache提高PHP程序性能的方法 2023-01-04
- ThinkPHP框架整合微信支付之JSAPI模式图文详解 2023-01-05
- PHP7中I/O模型内核剖析详解 2023-01-07
- PHP设计模式中工厂模式深入详解 2023-07-04
- php的闭包(Closure)匿名函数详解 2024-01-04
- 详解php用static方法的原因 2022-11-20
- 如何利用微信小程序和php实现即时通讯聊天功能 2023-06-26
- PHP内存溢出的解决方法详解 2023-06-26