gpt4 book ai didi

PHP-MySQL结果转换为JSON

转载 作者:行者123 更新时间:2023-12-03 23:02:33 28 4
gpt4 key购买 nike

我试图了解如何将MySQL结果转换为JSON格式,以便以后可以在Javascript中使用此JSON来构建HTML表。但是我的代码只是产生大量的空值,我还不明白为什么。

$result = mysqli_query($con, "SELECT * FROM Customers");

$test = json_encode($result);

print $test;

输出:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}

例如,我有诸如“CustomerID”和“Name”之类的字段,甚至它们都没有出现在JSON结果中。

我究竟做错了什么?
谢谢

最佳答案

$result = mysqli_query($con, "SELECT * FROM Customers");   
while($row = mysqli_fetch_assoc($result))
$test[] = $row;
print json_encode($test);

关于PHP-MySQL结果转换为JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23201212/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com