doob0526 2015-06-15 14:15
浏览 35
已采纳

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.

  • 写回答

1条回答 默认 最新

  • douchui4815 2015-06-15 14:35
    关注

    "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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信