以下是我的PDO
脚本的一部分,该脚本从我的网页获取AJAX POST
个请求。到目前为止,我已经按预期工作了,但是现在我想添加一个简单的for循环,它通过我的$results
数组进行迭代,并在strtotime
键下的时间戳上运行"posted"
{1}}。
现在,每当我取消注册for-loop时,整个事情就会刹车,我无法弄清楚原因。我得到的只是HTTP/1.0 500 Internal Server Error
。
对于发生了什么以及如何解决这个问题的任何解释都表示赞赏。
我的代码(我已经注释掉了不相关的部分):
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
$hostname = "localhost";
$username = "topdecka_admin";
$password = "";
$db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC;charset=utf8",$username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*if (!empty($_POST["searchword"])) {
$searchword = $_POST["searchword"];
$query = $db->prepare(
'SELECT articles.title, articles.posted, articles.extract, authors.name, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles, authors, categories, article_categories
WHERE articles.author_id = authors.id
AND articles.id = article_categories.article_id
AND article_categories.category_id = categories.id
AND ((title LIKE :searchword) OR (extract LIKE :searchword) OR (body LIKE :searchword) OR (name LIKE :searchword) OR (cat_name LIKE :searchword))'
); //end DB QUERY
$query->execute(array(":searchword" => "%" . $searchword . "%"));
$result = $query->fetchAll();
//turns timestamp into integer
for($i = 0; $i < count($result); ++$i) {
$result[$i]['posted'] = strtotime($result[$i]['posted']);
}
echo json_encode($result);
die();
}
else if (!empty($_POST["title"])) {
$title = $_POST["title"];
$query = $db->prepare(
"SELECT articles.title, articles.posted, articles.body, authors.name, authors.img, authors.bio, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles INNER JOIN authors ON articles.author_id = authors.id
INNER JOIN article_categories ON articles.id = article_categories.article_id
INNER JOIN categories ON article_categories.category_id = categories.id
WHERE title LIKE :title; SELECT comment.user_id, comment.text, comment.posted FROM articles RIGHT JOIN comment ON articles.id = comment.article_id
WHERE title LIKE :title;"
); //end DB QUERY
$query->execute(array(":title" => $title));
$result = $query->fetchAll();
$result[0]['posted'] = strtotime($result[0]['posted']);
$query->nextRowset();
$result2 = $query->fetchAll();
for($x=0; $x < count($result2); $x++) {
$result2[$x]['posted'] = strtotime($result2[$x]['posted']);
}
echo json_encode(array('article'=>$result, 'comments'=>$result2));
die();
} */
else if (!empty($_POST["comment_load"])) {
$comment_load = $_POST["comment_load"];
$query = $db->prepare(
"SELECT comment.user_id, comment.text, comment.posted FROM articles RIGHT JOIN comment ON articles.id = comment.article_id
WHERE title LIKE :comment_load;"
);
$query->execute(array(":comment_load" => $comment_load));
$result = $query->fetchAll();
for($x=0; x<count($result); x++) {
$result[$x]['posted'] = strtotime($result[$x]['posted']);
}
echo json_encode($result);
die();
}
/*else if (!empty($_POST["comment-text"])) {
$input_text = $_POST["comment-text"];
$query = $db->prepare(
'INSERT INTO comment (user_id, text, article_id)
VALUES (101, :input_text, 4);'
);
$query->execute(array(":input_text" => $input_text));
echo json_encode($result);
die();
}
else {
$query = $db->prepare(
'SELECT articles.title, articles.posted, articles.extract, authors.name, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles, authors, categories, article_categories
WHERE articles.author_id = authors.id
AND articles.id = article_categories.article_id
AND article_categories.category_id = categories.id'
); //end DB QUERY
$query->execute();
$result = $query->fetchAll();
//turns timestamp into integer
for($i = 0; $i < count($result); ++$i) {
$result[$i]['posted'] = strtotime($result[$i]['posted']);
}
echo json_encode($result);
die();
}*/
}
catch (PDOException $e) {
echo "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
这是没有for循环更改时间戳的var_dump($result)
:
"array(7) {
[0]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(4) "miha"
[1]=>
string(4) "miha"
["posted"]=>
string(19) "2015-06-15 10:18:44"
[2]=>
string(19) "2015-06-15 10:18:44"
}
[1]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(4) "miha"
[1]=>
string(4) "miha"
["posted"]=>
string(19) "2015-06-15 10:19:21"
[2]=>
string(19) "2015-06-15 10:19:21"
}
[2]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(14) "miha miha miha"
[1]=>
string(14) "miha miha miha"
["posted"]=>
string(19) "2015-06-15 15:57:40"
[2]=>
string(19) "2015-06-15 15:57:40"
}
[3]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(14) "miha miha miha"
[1]=>
string(14) "miha miha miha"
["posted"]=>
string(19) "2015-06-15 15:59:05"
[2]=>
string(19) "2015-06-15 15:59:05"
}
[4]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(17) "miha miha miha..."
[1]=>
string(17) "miha miha miha..."
["posted"]=>
string(19) "2015-06-15 15:59:35"
[2]=>
string(19) "2015-06-15 15:59:35"
}
[5]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(20) "miha miha miha......"
[1]=>
string(20) "miha miha miha......"
["posted"]=>
string(19) "2015-06-15 16:00:15"
[2]=>
string(19) "2015-06-15 16:00:15"
}
[6]=>
array(6) {
["user_id"]=>
string(3) "101"
[0]=>
string(3) "101"
["text"]=>
string(20) "miha miha miha......"
[1]=>
string(20) "miha miha miha......"
["posted"]=>
string(19) "2015-06-15 16:11:12"
[2]=>
string(19) "2015-06-15 16:11:12"
}
}
感谢您的帮助。
答案 0 :(得分:1)
"yeh, the problem was with the missing $ signs when declaring x in the loop. Write it up as an anwser and I'll accept it. Thanks"
As per OP's request, comment to answer:
this for($x=0; x<count($result); x++) {
you're missing $
signs for the x
's
The x
is treated as a constant and error reporting would have told you that.
Undefined constant x...
Check your logs. Plus, you have something similar that is commented out in for($i = 0; $i < count($result); ++$i) {
so do something similar to that, but using x
rather than i