我的代码在本地100%工作,返回的答案如下: [{ “ID”: “1”, “zuzycie”: “40”},{ “ID”: “3”, “zuzycie”: “83.333333333333”}]。 服务器上有相同的文件和代码结构。我连接数据库没有问题,所以路径很好。但是服务器上的代码返回[null,null,null,null,null]。有谁知道如何解决它?
<?php
//setting header to json
header('Content-Type: application/json');
require_once "../polaczenie.php";
//get connection
$mysqli = @new mysqli($host,$db_user,$db_password,$db_name);
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
//query to get data from the table
$query = sprintf("SELECT id, zuzycie FROM zuzycie_paliwa ORDER BY id ");
//execute query
$result = $mysqli->query($query);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
//free memory associated with result
$result->close();
//close connection
$mysqli->close();
//now print the data
print json_encode($data);
?>
答案 0 :(得分:1)
我怀疑它是否在本地工作,因为你正在尝试迭代mysqli结果。尝试用
替换你的foreach循环while ($row = $result->fetch_assoc()) {
$data[] = $row;
}