title: php PDO for loop brakes code
body: <p>The following is part of my <code>PDO</code> script that gets <code>AJAX POST</code> 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 <code>$results</code> array, and runs <code>strtotime</code> on a timestamp that's under the key of <code>"posted"</code>. </p>
  
  <p>Now every time I uncomments my for-loop the whole thing brakes, and I can't figure out why. All I get is <code>HTTP/1.0 500 Internal Server Error</code>. </p>
  
  <p>Any explanation on what is going on and how to fix this is appreciated.</p>
  
  <p>my code (I've commented out the irrelevant parts):</p>
  
  <pre><code>&lt;?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-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
      /*if (!empty($_POST["searchword"])) {
          $searchword = $_POST["searchword"];
          $query = $db-&gt;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-&gt;execute(array(":searchword" =&gt; "%" . $searchword . "%"));
          $result = $query-&gt;fetchAll();
          //turns timestamp into integer
          for($i = 0; $i &lt; 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-&gt;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-&gt;execute(array(":title" =&gt; $title));
          $result = $query-&gt;fetchAll();
          $result[0]['posted'] = strtotime($result[0]['posted']);
          $query-&gt;nextRowset();
          $result2 = $query-&gt;fetchAll();
  
          for($x=0; $x &lt; count($result2); $x++) {
              $result2[$x]['posted'] = strtotime($result2[$x]['posted']);
          }
  
          echo json_encode(array('article'=&gt;$result, 'comments'=&gt;$result2));
          die();
      } */
  
      else if (!empty($_POST["comment_load"])) {
          $comment_load = $_POST["comment_load"];
          $query = $db-&gt;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-&gt;execute(array(":comment_load" =&gt; $comment_load));   
          $result = $query-&gt;fetchAll();
          for($x=0; x&lt;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-&gt;prepare(
              'INSERT INTO comment (user_id, text, article_id)
              VALUES (101, :input_text, 4);'
              );
          $query-&gt;execute(array(":input_text" =&gt; $input_text));
          echo json_encode($result);
          die();
      }
      else {
          $query = $db-&gt;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-&gt;execute();
          $result = $query-&gt;fetchAll();
          //turns timestamp into integer
          for($i = 0; $i &lt; count($result); ++$i) {
            $result[$i]['posted'] = strtotime($result[$i]['posted']);
          }
          echo json_encode($result);
          die();
      }*/
  } 
  catch (PDOException $e) {
      echo "Error!: " . $e-&gt;getMessage() . "&lt;br/&gt;";
      die();
  }
  ?&gt;
  </code></pre>
  
  <p>Here's the <code>var_dump($result)</code> without the for-loop changing the timestamp:</p>
  
  <pre><code>"array(7) {
    [0]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(4) "miha"
      [1]=&gt;
      string(4) "miha"
      ["posted"]=&gt;
      string(19) "2015-06-15 10:18:44"
      [2]=&gt;
      string(19) "2015-06-15 10:18:44"
    }
    [1]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(4) "miha"
      [1]=&gt;
      string(4) "miha"
      ["posted"]=&gt;
      string(19) "2015-06-15 10:19:21"
      [2]=&gt;
      string(19) "2015-06-15 10:19:21"
    }
    [2]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(14) "miha miha miha"
      [1]=&gt;
      string(14) "miha miha miha"
      ["posted"]=&gt;
      string(19) "2015-06-15 15:57:40"
      [2]=&gt;
      string(19) "2015-06-15 15:57:40"
    }
    [3]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(14) "miha miha miha"
      [1]=&gt;
      string(14) "miha miha miha"
      ["posted"]=&gt;
      string(19) "2015-06-15 15:59:05"
      [2]=&gt;
      string(19) "2015-06-15 15:59:05"
    }
    [4]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(17) "miha miha miha..."
      [1]=&gt;
      string(17) "miha miha miha..."
      ["posted"]=&gt;
      string(19) "2015-06-15 15:59:35"
      [2]=&gt;
      string(19) "2015-06-15 15:59:35"
    }
    [5]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(20) "miha miha miha......"
      [1]=&gt;
      string(20) "miha miha miha......"
      ["posted"]=&gt;
      string(19) "2015-06-15 16:00:15"
      [2]=&gt;
      string(19) "2015-06-15 16:00:15"
    }
    [6]=&gt;
    array(6) {
      ["user_id"]=&gt;
      string(3) "101"
      [0]=&gt;
      string(3) "101"
      ["text"]=&gt;
      string(20) "miha miha miha......"
      [1]=&gt;
      string(20) "miha miha miha......"
      ["posted"]=&gt;
      string(19) "2015-06-15 16:11:12"
      [2]=&gt;
      string(19) "2015-06-15 16:11:12"
    }
  }
  </code></pre>
  
  <p>Thanks for the help.</p>