Create a table from MySQL using PHP PDO I am trying to simply present a table in HTML that is stored in a MySQL database. I would like to use Object Oriented PHP to access and fetch the data for the table. I have spent some time learning the different elements and have tried to put together a generic template I can use to access the tables in the database.

Questions:


Is there anything wrong with the code below?
Is there a better way to do this?
Are there any redundancies in the code? Is a more generally preferred/standard way of doing this? I've seen foreach and while being used...




&lt;html&gt;
&lt;table&gt;
&lt;tr&gt;
    &lt;th&gt;field1&lt;/th&gt;
    &lt;th&gt;field2&lt;/th&gt;
    &lt;th&gt;field3&lt;/th&gt;
    &lt;th&gt;field4&lt;/th&gt;
    &lt;th&gt;field5&lt;/th&gt;
&lt;/tr&gt;
&lt;?php
require_once 'db_config.php';
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sth = $dbh-&gt;prepare("SELECT * FROM a_temp");
$sth-&gt;execute();
$result = $sth-&gt;fetch(PDO::FETCH_ASSOC);
?&gt;
&lt;?php foreach($result as $index =&gt; $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;
&lt;/body&gt;
&lt;/html&gt;
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;