2015-03-19 116 views
2

我不明白为什么我的php什么都不显示。 我有一个数据库电影,我想把基地的元素放在一个JSON中。 但是,当我用我的localhost测试php时,我什么都没有。PHP MySQL Json_encode什么都不显示

我的数据库电影:

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”:[]}

感谢您的帮助

迈克尔

+0

您是否可以通过将页面更改为die(“我在这里!”)来确认页面正在进入最终die()行; – 2015-03-19 11:47:39

+0

当我改变死亡(“我是她!”);什么也没有显示。 – mickey74 2015-03-19 11:53:34

+0

这表明问题在别处。在<?php error_reporting(E_ALL);之后,将此权限添加到脚本的开头。 ini_set('display_errors',1); – 2015-03-19 11:54:49

回答

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; 
?> 

它是怎么了?你应该从thsi看到,或者实际上它是这个脚本这是问题的全部。

其他信息

好吧,我把我自己的意见和你不够好,提供表定义和一些示例数据我创建了一个数据库,将数据加载和运行代码。

它运行完成并生成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不会将其理解为日期。

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

+0

然后将'die(json_encode($ row1));'改为'echo json_encode($ row1);退出;'就像我在我的回答中所做的那样。 – RiggsFolly 2015-03-19 12:46:32

+0

这是工作,我已经改变了日期。所以,谢谢你的帮助。感谢 – mickey74 2015-03-19 12:47:18

+0

和'title'的列定义? – RiggsFolly 2015-03-19 12:47:46

0

json_encode()手册页:

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

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

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

我希望看到charset=utf8否则在连接字符串中的东西。