我试图了解如何将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/