gpt4 book ai didi

PHP MySQL Json_encode 什么都不显示

转载 作者:行者123 更新时间:2023-11-29 03:32:10 24 4
gpt4 key购买 nike

我不明白为什么我的 php 什么都不显示。我有一个数据库电影,我想将基础元素放入 JSON 中。然而,当我用我的本地主机测试 php 时,我什么都没有。

我的数据库电影:

DROP TABLE IF EXISTS `Movies` ;

CREATE TABLE IF NOT EXISTS `Movies` (
`classment` INT NOT NULL,
`title` VARCHAR(45) NOT NULL,
`actors` VARCHAR(45) NOT NULL,
`kind` VARCHAR(45) NOT NULL,
`director` VARCHAR(45) CHARACTER SET 'big5' NOT NULL,
`date` DATE NOT NULL,
`image` BLOB NULL,
`summary` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`classment`))
ENGINE = InnoDB;

我将两部电影插入数据库:

INSERT INTO Movies (classment, title, actors, kind, director, date, image, summary)
VALUES ('1', 'The Lord of the rings : The fellowship of the rings', 'Elijah Wood, Ian McKellen, Viggo Mortensen', 'Adventure/Fantasy', 'Peter Jackson','19/12/2001', 'LOAD_FILE("/home/michael/Documents/Dkit/Master1/Semester2/Enterprise_Mobility/CA2/images/LoR1.jpeg")', 'A meek hobbit of the Shire and eight companions set out on a journey to Mount Doom to destroy the One Ring and the dark lord Sauron.' );

INSERT INTO Movies (classment, title, actors, kind, director, date, image, summary)
VALUES ('2', 'The Lord of the rings : The two towers', 'Elijah Wood, Ian McKellen, Viggo Mortensen', 'Adventure/Fantasy', 'Peter Jackson','18/12/2002', 'LOAD_FILE("/home/michael/Documents/Dkit/Master1/Semester2/Enterprise_Mobility/CA2/images/LoR2.jpeg")', 'While Frodo and Sam edge closer to Mordor with the help of the shifty Gollum, the divided fellowship makes a stand against Sauron\'s new ally, Saruman, and his hordes of Isengard.' );

还有我的 PHP 代码:

<?php

try{
$handler = new PDO('mysql:host=localhost;dbname=movies', 'root', 'xxxx');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(Exception $e){
echo $e->getMessage();
die();
}

// query the application data
$query = $handler->prepare('SELECT title, image FROM Movies');

//execute the prepared statement
$query->execute();

// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
$rows[] = $row;
}

$row1["ListMovies"] = $rows;
die(json_encode($row1));
?>

我想获得这样的东西:{"ListMovies":[]}

谢谢你的帮助

迈克尔

最佳答案

由于您的 php 代码不需要输入,您可以像普通页面一样从浏览器运行它。

所以向它添加一些调试消息,这样你就可以看到它到达了哪里,如果有的话。

<?php
echo 'HELLO<br>';

try{
echo 'TRYING<br>';
$handler = new PDO('mysql:host=localhost;dbname=movies', 'root', 'xxxx');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(Exception $e){
echo $e->getMessage();
die();
}

// query the application data
echo 'BEFORE PREPARE<br>';

$query = $handler->prepare('SELECT title, image FROM Movies');

echo 'AFTER PREPARE<br>';

//execute the prepared statement
$query->execute();

echo 'AFTER EXECUTE<br>';

// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<pre>In While' . print_r($row,true) . '</pre>';
$rows[] = $row;
}

$row1["ListMovies"] = $rows;
echo '<pre>' . print_r($row1,true) . '</pre>';

echo json_encode($row1);
exit;
?>

您应该从中看出哪里出错了,或者实际上问题是否出在这个脚本上。

附加信息

好吧,我采纳了自己的建议,由于您非常友好地提供了表定义和一些示例数据,我创建了该数据库并加载了数据并运行了您的代码。

它一直运行到完成并生成了 json 字符串数据。所以这段代码不是你的问题。

结果

HELLO
TRYING
BEFORE PREPARE
AFTER PREPARE
AFTER EXECUTE

In WhileArray
(
[title] => The Lord of the rings : The fellowship of the
[image] => LOAD_FILE("/home/michael/Documents/Dkit/Master1/Semester2/Enterprise_Mobility/CA2/images/LoR1.jpeg")
)

In WhileArray
(
[title] => The Lord of the rings : The two towers
[image] => LOAD_FILE("/home/michael/Documents/Dkit/Master1/Semester2/Enterprise_Mobility/CA2/images/LoR2.jpeg")
)

Array
(
[ListMovies] => Array
(
[0] => Array
(
[title] => The Lord of the rings : The fellowship of the
[image] => LOAD_FILE("/home/michael/Documents/Dkit/Master1/Semester2/Enterprise_Mobility/CA2/images/LoR1.jpeg")
)

[1] => Array
(
[title] => The Lord of the rings : The two towers
[image] => LOAD_FILE("/home/michael/Documents/Dkit/Master1/Semester2/Enterprise_Mobility/CA2/images/LoR2.jpeg")
)

)

)

{"ListMovies":[{"title":"The Lord of the rings : The fellowship of the","image":"LOAD_FILE(\"\/home\/michael\/Documents\/Dkit\/Master1\/Semester2\/Enterprise_Mobility\/CA2\/images\/LoR1.jpeg\")"},{"title":"The Lord of the rings : The two towers","image":"LOAD_FILE(\"\/home\/michael\/Documents\/Dkit\/Master1\/Semester2\/Enterprise_Mobility\/CA2\/images\/LoR2.jpeg\")"}]}

我建议您查看表格设计,因为您正在将超过 45 个字符加载到 title 中,并且您的 INSERT 中的 date 列定义不正确。

即插入的日期应采用 'yyyy-mm-dd' 格式,否则 MySQL 无法将其理解为日期。

关于PHP MySQL Json_encode 什么都不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com