Create a table from MySQL using PHP PDO
The fetch() call only calls a single row from result set.
Your foreach loop will, as a result, just go in the first result and do nothing. It may even produce an error/notice for undefined index in $row.
While using a SELECT statement in PHP, always include the specific columns that you want to fetch. This is among good practices.




$sth = $dbh-&gt;prepare("SELECT `field1`, `field2`, `field3`, `field4`, `field5` FROM a_temp");
$sth-&gt;execute();
?&gt;
&lt;?php foreach($sth-&gt;fetch(PDO::FETCH_ASSOC) as $row) : ?&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;?php echo $row['field1']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field2']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field3']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field4']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field5']; ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php endforeach;?&gt;
&lt;/table&gt;




Or, if you want to use a while loop:

$sth = $dbh-&gt;prepare("SELECT `field1`, `field2`, `field3`, `field4`, `field5` FROM a_temp");
$sth-&gt;execute();
?&gt;
&lt;?php while( $row = $sth-&gt;fetch(PDO::FETCH_ASSOC) ) { ?&gt;
&lt;tr&gt;
    &lt;td&gt;&lt;?php echo $row['field1']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field2']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field3']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field4']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['field5']; ?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php } ?&gt;
&lt;/table&gt;