以下是我的PDO
腳本的一部分,該腳本從我的網頁獲取AJAX POST
請求。到目前爲止,我已經按照滿意度工作,但現在我想添加一個簡單的for循環,通過我的$results
陣列進行迭代,並在"posted"
的密鑰下運行strtotime
。php PDO for循環剎車代碼
現在每次我取消註釋我的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();
}
?>
這裏的var_dump($result)
沒有for循環變化的時間戳:
"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"
}
}
感謝您的幫助。
這個for($ x = 0; x
你好,問題是在循環中聲明x時缺少$符號。把它寫成一個anwser,我會接受它。謝謝 –
檢查服務器日誌中有關500的詳細信息。這應該是你第一次停止任何時候你得到一個500. –