php PDO for循环刹车代码
The following is part of my PDO
script that gets AJAX POST
requests from my webpage. So far I've got it working as intented, but now I would like to add a simple for-loop that iterates trough my $results
array, and runs strtotime
on a timestamp that's under the key of "posted"
.
Now every time I uncomments my for-loop the whole thing brakes, and I can't figure out why. All I get is HTTP/1.0 500 Internal Server Error
.
Any explanation on what is going on and how to fix this is appreciated.
my code (I've commented out the irrelevant parts):
<?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();
}
?>
Here's the var_dump($result)
without the for-loop changing the timestamp:
"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"
}
}
Thanks for the help.
"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