PHP Mysqli 从数据库中读取对象数组转换为 JSON
PHP Mysqli Reading Array of Objects from Database converting to JSON
$query = "SELECT guid, name, location, description, datetime FROM event";
$result = doSQLTermError($db_connect, $query);
while($obj = mysqli_fetch_object($result)){
$guid = $obj->guid;
$event_details = new stdClass;
$event_details->$guid = new stdClass;
$event_details->$guid->name = $obj->name;
$event_details->$guid->location = $obj->location;
$event_details->$guid->description = $obj->description;
$event_details->$guid->datetime = $obj->datetime;
}
$event = json_encode($event_details);
上面的代码为我提供了以下所需的 JSON 输出:
"data":
{
"f4ef863ef80b07036de08457ccdf51e3f814":
{
"name":"adsg",
"location":"uhjty;",
"description":"y;hy",
"datetime":"2018-01-01 00:00:00"
}
}
但是它只从数据库中读取最后一个事件记录。如何列出数据库中的所有事件但保持上述 JSON 格式?谢谢。
将 $event_details = new stdClass;
移出 while 循环。
您每次都用新实例覆盖 $event_details
变量,因此您丢失了上一个循环中已经存在的数据。
您必须使用数组从 SQL 查询中获取所有数据:
$query = "SELECT guid, name, location, description, datetime FROM event";
$result = doSQLTermError($db_connect, $query);
$events = [];
while($obj = mysqli_fetch_object($result)){
$guid = $obj->guid;
$event_details = new stdClass;
$event_details->$guid = new stdClass;
$event_details->$guid->name = $obj->name;
$event_details->$guid->location = $obj->location;
$event_details->$guid->description = $obj->description;
$event_details->$guid->datetime = $obj->datetime;
$events[] = json_encode($event_details);
}
$query = "SELECT guid, name, location, description, datetime FROM event";
$result = doSQLTermError($db_connect, $query);
$event_details = new stdClass;
while($obj = mysqli_fetch_object($result)){
$guid = $obj->guid;
$event_details->$guid = new stdClass;
$event_details->$guid->name = $obj->name;
$event_details->$guid->location = $obj->location;
$event_details->$guid->description = $obj->description;
$event_details->$guid->datetime = $obj->datetime;
}
$event = json_encode($event_details);
$query = "SELECT guid, name, location, description, datetime FROM event";
$result = doSQLTermError($db_connect, $query);
while($obj = mysqli_fetch_object($result)){
$guid = $obj->guid;
$event_details = new stdClass;
$event_details->$guid = new stdClass;
$event_details->$guid->name = $obj->name;
$event_details->$guid->location = $obj->location;
$event_details->$guid->description = $obj->description;
$event_details->$guid->datetime = $obj->datetime;
}
$event = json_encode($event_details);
上面的代码为我提供了以下所需的 JSON 输出:
"data":
{
"f4ef863ef80b07036de08457ccdf51e3f814":
{
"name":"adsg",
"location":"uhjty;",
"description":"y;hy",
"datetime":"2018-01-01 00:00:00"
}
}
但是它只从数据库中读取最后一个事件记录。如何列出数据库中的所有事件但保持上述 JSON 格式?谢谢。
将 $event_details = new stdClass;
移出 while 循环。
您每次都用新实例覆盖 $event_details
变量,因此您丢失了上一个循环中已经存在的数据。
您必须使用数组从 SQL 查询中获取所有数据:
$query = "SELECT guid, name, location, description, datetime FROM event";
$result = doSQLTermError($db_connect, $query);
$events = [];
while($obj = mysqli_fetch_object($result)){
$guid = $obj->guid;
$event_details = new stdClass;
$event_details->$guid = new stdClass;
$event_details->$guid->name = $obj->name;
$event_details->$guid->location = $obj->location;
$event_details->$guid->description = $obj->description;
$event_details->$guid->datetime = $obj->datetime;
$events[] = json_encode($event_details);
}
$query = "SELECT guid, name, location, description, datetime FROM event";
$result = doSQLTermError($db_connect, $query);
$event_details = new stdClass;
while($obj = mysqli_fetch_object($result)){
$guid = $obj->guid;
$event_details->$guid = new stdClass;
$event_details->$guid->name = $obj->name;
$event_details->$guid->location = $obj->location;
$event_details->$guid->description = $obj->description;
$event_details->$guid->datetime = $obj->datetime;
}
$event = json_encode($event_details);