//演示用过程化风格,如需要,自行翻译成面向对象风格
//让浏览器识别json
header('Content-type: application/json');
//1、连接数据库
$conn = mysqli_connect("localhost","my_user","my_password","my_db");
//2、查询
$query= mysqli_query($conn,"select * form table where 1 limit 10");
$res = [];
//3、将查询结果赋值给变量res
while($res = mysqli_fetch_assoc($query)){
$res[] = $res;
}
//4、输出json
echo json_encode($res);
温馨提示:内容为网友见解,仅供参考