PHP MySQL Json_encode不显示任何内容


PHP MySQL Json_encode display nothing

我不明白为什么我的php什么都不显示。我有一个dataBaseMovies,我想把这个数据库的元素放在一个JSON中。然而,当我用localhost测试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":[]}

感谢您的帮助

Michaël

由于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不将其理解为日期。

我有这个:

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")
            )
    )
  ) 

但没有json_encode

来自json_encode()手册页面:

所有字符串数据都必须使用UTF-8编码。

您的代码中没有任何对UTF-8的引用。当你连接到数据库时,你甚至没有设置编码:

new PDO('mysql:host=localhost;dbname=movies', 'root', 'xxxx');
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我希望在连接字符串中看到charset=utf8或其他内容。