title: Query does not fetch data while it executes correctly
body: <p>I find it very strange but something does not feel right. I want to populate a JSON Array with data from mysql. The first query will bring data for categories and questions and then for each question i want to get the answers. I get the data from 1st query but from second i do not.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL ^ E_NOTICE);
  ini_set("default_charset", "UTF-8");
  header('Content-type: text/html; charset=UTF-8');
  try {
      $handler = new PDO('mysql:host=localhost;dbname=database', 'root', '');
      $handler-&gt;setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8");
      $handler-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $handler-&gt;exec("SET CHARACTER SET 'utf8'"); 
  } catch (Exception $e) {
      echo $e-&gt;getMessage();
      die();
  }
  
  $query = $handler-&gt;query('SELECT DISTINCT c.cat_name, c.cat_id, q.question FROM `categories` c
  LEFT JOIN `questions` q ON c.cat_id = q.cat_id WHERE c.cat_id = 1');
  $records = array();
  
  $records = $query-&gt;fetchAll(PDO::FETCH_ASSOC);
  echo "&lt;pre&gt;";
  print_r($records);
  echo "&lt;/pre&gt;";
  $answers = array();
  foreach($records as $k =&gt; $v){
      $ques = $v['question'];
      $ques = trim($ques);
      $qu = $handler-&gt;query("SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."' ");
      echo "SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."'&lt;br&gt;";
      $answers = $qu-&gt;fetch(PDO::FETCH_BOTH);
      /*$answers = $qu-&gt;fetchAll(PDO::FETCH_ASSOC);
      foreach ($answers as $key =&gt; $value) {
          echo "Key: " . $key . " Value: " . $value;
      }
      //$answersR = $qu-&gt;fetchAll(PDO::FETCH_ASSOC);*/
      echo "&lt;pre&gt;";
      print_r($answers);
      echo "&lt;/pre&gt;";
  }
  $j['quiz'] = $json;
  echo json_encode($j);
  /*$json[] = array(
          "category_name" =&gt; $v['cat_name'], "category_id" =&gt; $v['cat_id'],  "question_name" =&gt; $v['question'],
          "answers" =&gt; array(
              "answer" =&gt; $answers['answer'],
              "iscorrect" =&gt; $answers['iscorrect']
      ));*/
  ?&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I managed to fix it with this code:</p>
  
  <pre><code>foreach($records as $k =&gt; $v){
      $a[] = array("category_name" =&gt; $v['cat_name'], "category_id" =&gt; $v['cat_id'],  "question_name" =&gt; $v['question'], "question_answers" =&gt; array() );
      $normal[] = $v['question'];
  }
  foreach ($normal as $key =&gt; $value) {
          $ques = $value;
          $qu = $handler-&gt;query("SELECT a.answer, a.iscorrect FROM `answers` a INNER JOIN `questions` q ON a.quest_id = q.q_id WHERE q.question = '".$ques."' ");
          $ans = $qu-&gt;fetchAll(PDO::FETCH_ASSOC);
          foreach ($ans as $key =&gt; $value) {
              $times[] = array('answer' =&gt; $value['answer'], 'iscorrect' =&gt; $value['iscorrect']);
          }
  }
  </code></pre>
  
  <p>Now i want for each item in the a array the array "question_answers" to be populated with the values of each item of the array times.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>foreach ($times as $w =&gt; $e) {
      $a['question_answers'][]  = array("answer" =&gt; $e['answer'], "iscorrect" =&gt; $e['iscorrect']);
  }
  </code></pre>
  
  <p>But it does not give me the desired result.</p>
  
  <p>I want result to be like this:</p>
  
  <pre><code>"category_name" =&gt; categoryname,
  "category_id" =&gt; categoryid,
  "question_name" =&gt; questionname,
  "question_answers" =&gt;[
      "answer" =&gt; answer1,
      "iscorrect" =&gt; yes,
  
      "answer" =&gt; answer2,
      "iscorrect" =&gt; no,
  
      "answer" =&gt; answer3,
      "iscorrect" =&gt; no,
  ]
  </code></pre>
  
  <p>How is this possible to do. With the last method i tried it does not work. gives me empty array.</p>
  
  <p>I have tried while loops and foreach but still nothing. I would appreaciate any help!</p>