如何将mysqli结果转换为JSON?

如何将mysqli结果转换为JSON?,第1张

如何将mysqli结果转换为JSON?
$mysqli = new mysqli('localhost','user','password','myDatabaseName');$myArray = array();if ($result = $mysqli->query("SELECt * FROM phase1")) {    while($row = $result->fetch_array(MYSQLI_ASSOC)) { $myArray[] = $row;    }    echo json_enpre($myArray);}$result->close();$mysqli->close();
  1. $row = $result->fetch_array(MYSQLI_ASSOC)
  2. $myArray[] = $row

这样的输出

[    {"id":"31","name":"pruduct_name1","price":"98"},    {"id":"30","name":"pruduct_name2","price":"23"}]

如果您想要其他样式,可以尝试以下方法:

  1. $row = $result->fetch_row()
  2. $myArray[] = $row

输出将是这样的:

[    ["31","pruduct_name1","98"],    ["30","pruduct_name2","23"]]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5018519.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存