title: Use PHP to Display MySQL Results in HTML Table
body: <p><strong>Update for CodingBiz:</strong></p>
  
  <p>I'm putting this in my code:</p>
  
  <pre><code>for($i=1;$i&lt;=$numRows;$i++) {
      $output .= '&lt;tr&gt;';
      $row = $this-&gt;fetchAssoc($result);
      $colRow = $this-&gt;fetchAssoc($colResult);
      foreach($colRow as $colName) {
          $output .= "&lt;td&gt;".$row[$colName]."&lt;/td&gt;";
      }
      $output .= '&lt;/tr&gt;';
  }
  </code></pre>
  
  <p>in place of </p>
  
  <pre><code>for($i=1;$i&lt;=$numRows;$i++) {
      $output .= '&lt;tr&gt;';
      $row = $this-&gt;fetchAssoc($result);
      for($j=1;$j&lt;=$colNumRows;$j++) {
          $colRow = $this-&gt;fetchAssoc($colResult);
          $output .= "&lt;td&gt;".$row[$colRow["COLUMN_NAME"]]."&lt;/td&gt;";
      }
      $output .= '&lt;/tr&gt;';
  }
  </code></pre>
  
  <p>Is there anything wrong with this?</p>
  
  <p><strong>Original Post:</strong></p>
  
  <p>I'm writing a function in a PHP class to display the results of a query in a table. I'm not structuring any of the table myself, I want it everything to be done using PHP. Here is my code so far:</p>
  
  <pre><code>function allResults($table,$cols) {
      if(isset($cols)) {
          $query = "SELECT $cols FROM $table";
      }
      else {
          $query = "SELECT * FROM $table";
      }
      $result = $this-&gt;query($query);
      $numRows =  $this-&gt;numRows($result);
      $colQuery ="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='shareride'  AND TABLE_NAME='$table'";
      $colResult = $this-&gt;query($colQuery);
      $colNumRows = $this-&gt;numRows($colResult);
  
      $output = '&lt;table class="allResults"&gt;';
      $output .= '&lt;tr&gt;';
      for($i=1;$i&lt;=$colNumRows;$i++) {
          $colRow = $this-&gt;fetchAssoc($colResult);
          $output .= "&lt;td&gt;".$colRow["COLUMN_NAME"]."&lt;/td&gt;";
      }
      $output .= '&lt;/tr&gt;';
      for($i=1;$i&lt;=$numRows;$i++) {
          $output .= '&lt;tr&gt;';
          $row = $this-&gt;fetchAssoc($result);
          for($j=1;$j&lt;=$colNumRows;$j++) {
              $colRow = $this-&gt;fetchAssoc($colResult);
              $output .= "&lt;td&gt;".$row[$colRow["COLUMN_NAME"]]."&lt;/td&gt;";
          }
          $output .= '&lt;/tr&gt;';
      }
      $output .= '&lt;/table&gt;';
      return $output;
  }
  </code></pre>
  
  <p>In case it is unclear, <code>query</code> refers to <code>mysqli_query</code>, <code>numRows</code> refers to <code>mysqli_num_rows</code>, and <code>fetchAssoc</code> refers to <code>mysqli_fetch_assoc</code>. The database name is "shareride."</p>
  
  <p>I know I am missing something in this line:</p>
  
  <pre><code>$output .= "&lt;td&gt;".$row[$colRow["COLUMN_NAME"]]."&lt;/td&gt;";
  </code></pre>
  
  <p>but I just don't know what it is. Right now, I get all the table column titles displayed correctly, and I get the correct number of content rows, but I just can't populate those rows with the actual data from the database.</p>
  
  <p>What am I missing? Any help would be <strong>GREATLY</strong> appreciated!</p>