解决了问题。谢谢大家。
代码:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM movie WHERE status = 'In Cinemas' LIMIT 6";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$moviename = $row['moviename'];
$cover = $row['poster'];
header('Content-Type: application/json; charset=utf-8');
$alldata [] = array('key'=>'movie-name', 'value'=>$moviename,'cover' =>$cover);
echo json_encode($alldata);
}}
else{
echo "does not work";
}
结果:
{ "items":
[
{"key":"movie-name","value":"test","cover":"upload/movies/poster/1.png"}
{"key":"movie-name","value":"London","cover":"upload/movies/poster/2.png"}
]}
问题:
没有逗号分隔json的两个输出,以{“key”开头 - 括号之前应该是逗号,请帮助实现
解决了问题。谢谢大家。
答案 0 :(得分:0)
$sql = "SELECT * FROM movie WHERE status = 'In Cinemas' LIMIT 6";
$result = $conn->query($sql);
$master_array = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$moviename = $row['moviename'];
$poster = $row['poster'];
$master_array[] = array('key'=>'movie-name','value'=>$moviename,'cover'=>$poster);
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($master_array);
}
else{
echo "no";
}