title: How to use foreach get data from the database table --
body: <p>My database has a table called <strong>tblprojects</strong> with column names say, <strong>project_num, project_status, project_name</strong>. I want to use foreach loop to get the data from the database and display the records in php table. </p>
  
  <p>However I am unable to display the records with following code. Please help me in correcting it. Am new to using PHP.</p>
  
  <p>Following is the code I have written:</p>
  
  <pre><code>&lt;?php
       $projects = array();
       // fetch data from the database
       $records = mysql_query('select project_num, project_status, project_name from tblprojects') or die("Query fail: " . mysqli_error());
  ?&gt;
  
  
  &lt;table  class="table table-striped table-condensed" id="tblData"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Project Name&lt;/th&gt;
              &lt;th&gt;Project Number&lt;/th&gt;
              &lt;th&gt;Project Status&lt;/th&gt;
         &lt;/tr&gt;
      &lt;/thead&gt;
  
      &lt;tbody&gt;
         &lt;?php 
              while (  $row =  mysql_fetch_assoc($records)    )
              {
                  $projects[] = $row;
                  foreach ($projects as $project):
        ?&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;?echo $project['proj_name']; ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?echo $proj['proj_num']; ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?echo $proj['proj_status']; ?&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
        &lt;?php endforeach; 
             }
        ?&gt;
  
  
      &lt;/tbody&gt;        
  &lt;/table&gt;
  </code></pre>
  
  <p>Please help me in solving the problem,reply with corrected code ( preferred ). Thanks a lot in advance. </p>