PHP MySQL Json_编码不显示任何内容
我不明白为什么我的php没有显示任何内容。 我有一个数据库,我想把基本元素放在JSON中。 然而,当我用本地主机测试php时,我什么都没有 我的数据库电影:PHP MySQL Json_编码不显示任何内容,php,mysql,Php,Mysql,我不明白为什么我的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` VARCHA
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;
?>
我建议您查看表的设计,因为您正在将45个以上的字符加载到title
中,并且插入的date
列定义不正确
i、 e.insert中的日期应为“yyyy-mm-dd”格式,或者MySQL不将其理解为日期。我有以下信息:
但是,在手册页面中没有json_编码:
所有字符串数据必须采用UTF-8编码
您的代码中没有任何对UTF-8的引用。连接到数据库时甚至不设置编码:
我希望在连接字符串中看到
charset=utf8
或其他内容。您是否可以通过将页面更改为die(“我在这里!”)来确认页面正在到达最终的die()行;当我因死亡而改变(“我就是她!”);也没有显示。这对我来说意味着问题在别处。将此权限添加到脚本的开头。在尝试之前,我已经放了{,对吗?是的,没错……很惊讶你没有看到任何东西,但我建议这个问题比json_编码部分更深,你的语法在我看来很好!然后将die(json_encode($row1));
更改为echo json_encode($row1);退出;
就像我在回答中所做的那样。这是工作,我已经更改了日期。因此,非常感谢您的帮助。谢谢。没关系,只需将答案标记为正确即可,以便其他人来搜索时知道
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\")"}]}
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")
)
)
)
new PDO('mysql:host=localhost;dbname=movies', 'root', 'xxxx');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^