此代码仅将最后一行放入json;
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "db_user";
$mysql_db_password = "db_password";
$mysql_db_database = "db_name";
$con = @mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password,
$mysql_db_database);
if (!$con) {
trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}
$var = array();
$sql = "SELECT id,name FROM db_table";
$result = mysqli_query($con, $sql);
while($obj = mysqli_fetch_object($result)) {
$var = $obj;
}
echo '{"users":'.json_encode($var).'}';
?>
我可以请求帮助来纠正我的代码中的错误吗?
答案 0 :(得分:0)
将其替换为:
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
$data = ['users' => $var];
echo json_encode($data);
这将使$ var成为一个对象数组,否则你将覆盖$ var值。
答案 1 :(得分:0)
在$var
$var = array();
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
document.getElementById('YOURFORMID').reset();