title: Store MySQL query result into a two-dimensional array
body: <p>I have not seen a similar question so I'm asking a new one. I'm also experimenting with using loops and arrays, so apologies if this is a really dumb question or really bad code. I was able to do achieve my desired result with about 50 more lines of code, but it was even more inelegant (I basically had each query in a hardcoded line, instead of using variable). Hence, I am trying to learn how to code this a bit more neatly to make it more efficient. I might be going down the wrong path though...</p>
  
  <p>I have a table with the following basic structure:</p>
  
  <pre><code>[id / username / email / content / side / category / rating]
  </code></pre>
  
  <p>Side can either be "yes" or "no" and Category can either be "red", "blue" or "green". These  are restricted in the submission form.</p>
  
  <p>I'm trying to execute the following query in a loop to pull all the required data and store them into a two-dimensional variable in php:</p>
  
  <pre><code>$dialecticSides = array("yes","no");
  $numberSides = 2;
  $dialecticRow = array("red","blue","green");
  $numberRows = 3;
  
  $dialectic_queryString = $dialectic_mysqlQuery = $dialectic_sqlResult = $dialectic_totalRows = array();
  
  for($x=0;$x&lt;$numberSides;$x++) {
      for ($y=0;$y&lt;$numberRows;$y++) {        
          $dialectic_queryString[$x][$y] = "SELECT * FROM ". $dialectic_sqlTable ." WHERE side = '" . $dialecticSides[$x]. "' AND category = '". $dialecticRow[$y]."' ORDER BY rating DESC";
          $dialectic_mysqlQuery[$x][$y] = mysql_query($dialectic_queryString[$x][$y], $commenting_conn) or die(mysql_error());
          $dialectic_sqlResult[$x][$y] = mysql_fetch_assoc($dialectic_mysqlQuery[$x][$y]);
          $dialectic_totalRows[$x][$y] = mysql_num_rows($dialectic_sqlResult[$x][$y]);
  
          var_dump($dialectic_sqlResult);
      }
  }
  </code></pre>
  
  <p>My desired output is something like this:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
            [0] =&gt; Array
               (
                 all the comments which are in row 0 (yes) and side 0 (red)
               )
            [1] =&gt; Array
               (
                 all the comments which are in row 0 (yes) and side 1 (blue)
               )
            [2] =&gt; Array
               (
                 all the comments which are in row 0 (yes) and side 2 (green)
               )
          )
      [1] =&gt; Array
          (
            [0] =&gt; Array
               (
                  all the comments which are in row 1 (no) and side 0 (red)
               )
            [1] =&gt; Array
               (
                 all comments which are in row 1 (no) and side 1 (blue)
               )
            [2] =&gt; Array
               (
                 all the comments which are in row 1 (no) and side 2 (green)
               )
          )
  )
  </code></pre>
  
  <p>Essentially, if I want to know if what I'm doing makes any sense at all or if I should give up and go back to hardcoding the queries to different result variables.</p>
  
  <p>My query is failing - the output of the query is null. What am I doing wrong? Thanks in advance!</p>
  
  <p>==== SOLVED it ====</p>
  
  <pre><code>for($x=0;$x&lt;$numberSides;$x++) {
  for ($y=0;$y&lt;$numberRows;$y++) {        
      $queryString[$x][$y] = "SELECT * FROM comments WHERE topic_id = '{$topicid}' AND side = '{$x}' AND row = '{$y}'";
      $result[$x][$y] = mysqli_query($db_connection,$queryString[$x][$y]) or die(mysqli_error($db_connection));
      while($eachcomment = mysqli_fetch_assoc($result[$x][$y])) {
          $array[$x][$y][] = $eachcomment;
      }
  }
  </code></pre>