2015-06-15 49 views
0

以下是我的PDO脚本的一部分,该脚本从我的网页获取AJAX POST请求。到目前为止,我已经按照满意度工作,但现在我想添加一个简单的for循环,通过我的$results阵列进行迭代,并在"posted"的密钥下运行strtotimephp 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" 
    } 
} 

感谢您的帮助。

+1

这个for($ x = 0; x

+1

你好,问题是在循环中声明x时缺少$符号。把它写成一个anwser,我会接受它。谢谢 –

+1

检查服务器日志中有关500的详细信息。这应该是你第一次停止任何时候你得到一个500. –

回答

1

“叶,问题是与环中宣布X时失踪$迹象。写它作为一个anwser,我会接受它。谢谢。”

由于每个操作的请求评论回答:

for($x=0; x<count($result); x++) {你错过$标志为x

x为t作为constant重新编号,错误报告会告诉你。

未定义的常量X ...

检查你的日志。另外,你有类似的东西在for($i = 0; $i < count($result); ++$i) {中被注释掉,所以做类似的事情,但是使用x而不是i