Abhijeet Sridhar Abhijeet Sridhar - 1 year ago 118
PHP Question

How to print the whole database table from phpmyadmin using PDO in php file

I am trying to print the whole database in tabular format in a php file using PDO. i have this database stored in phpmyadmin. But there are a lot of rows in there like name,id, etc etc... I have this following php code. i already made a connection to the database and added require_once() in the page. But i dont know how to print all these values in a tabular method. like showing it in a way a normal database will look like.

$q="SELECT * FROM `employee`";

$sth = $odb->prepare($q);

while ($r = $sth->fetch(PDO::FETCH_ASSOC)){
// code here

Can someone help me to display the table properly. That is if i run it in browser, i should see a table instead of that ugly array format

Max Max
Answer Source

Ideally each table should have his own format, but if you just want to pop all the data from the base inside a HTML table, you could do something like that :

$sql = 'SELECT * from page';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);

if(count($result)) {
    echo '<table><tr>';
    foreach ($rows[0] as $columnName => $value) {
        echo '<th>' . $columnName . '</th>';
    echo '</tr>';
    foreach ($rows as $row) {
        echo '<tr>';
        foreach ($row as $value) {
            echo '<td>' . $value . '</td>';
        echo '<tr>';
    echo '</table>';

You could use this on each table you want to show.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download