title: Print a MySQL table to a dynamic HTML table with PHP
body: <p>I am teaching myself PHP and so far it has been going great. I am kinda stuck now and so in need of experts intervention here. I am trying to print a MySQL table to a dynamic HTML table with PHP. I want to display the column names and the rows based on the number of fields(colomns) and rows the query fetches. I have tried to iterate through the mysqli_fetch_row() for the rows and used a foreach on mysqli_fetch_fields(). But there is no output nor are there any errors. My novice eyes can't seem to detect any problem. Please help. Here is a part of my code:</p>
  
  <pre><code>if(isset($_POST['submit']))
  {
     $search=$_POST['name'];
     $conn=DBconnect();
     $result=SQLquery($conn, $search);
  
     if ($result-&gt;num_rows &gt; 0) 
     {
        echo "&lt;table id='tbl'&gt;&lt;tr&gt;";
        $field=$result-&gt;fetch_fields();
  // output column names  
       foreach ($field as $col)
       {
          echo "&lt;th&gt;".$col-&gt;name."&lt;/th&gt;";
       }
       echo "&lt;/tr&gt;"
  
  // output data of each row
       while($row = $result-&gt;fetch_row()) 
       {
          echo "&lt;tr&gt;";
  
          for ($i=0;$i&lt;=$result-&gt;field_count;$i++)
          {
             echo "&lt;td&gt;".$row[$i]."&lt;/td&gt;";
          }
  
          echo "&lt;/tr&gt;";
        }
       echo "&lt;/table&gt;";
  
    }
  
  else  
  {
   echo "No data found";
  }
  
  }
  </code></pre>
  
  <p>Your help is much appreciated. Thanks in advance! :)</p>