PHP MySQL Json_encode什么都没显示

时间:2015-03-19 11:43:20

标签: php mysql

我不明白为什么我的php没有显示任何内容。 我有一个数据库电影,我想把基础的元素放在一个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”:[]}

感谢您的帮助

迈克尔

3 个答案:

答案 0 :(得分:0)

由于您的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列。

即。 insert中的日期应采用'yyyy-mm-dd'格式,否则MySQL不会将其理解为日期。

答案 1 :(得分:0)

我有这个:

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

答案 2 :(得分:0)

json_encode()手册页:

  

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

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

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

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