Print all the Data from a MySQL Table in an HTML Table using PHP

What we are using here:

MySQL database:

–MySQL Username: root

–No MySQL Password

–Database Name: PracticeDatabase

–Table Name: Contacts

And 3 echo statements to print the HTML table.

The code:

 

Output:

Have a better way of doing this? Let us know and we will update it here.

2 thoughts on “Print all the Data from a MySQL Table in an HTML Table using PHP

  1. A more versatile method would be to automatically fetch the field names, so you could create a reusable function that will return the data for any table:

    function dbTableToHtml($table,$db){
    $query=mysql_query(‘explain '.$table.'‘,$db);
    $cols=array();
    while($row=mysql_fetch_assoc($query)){
    $cols[]=$row['Field'];
    }

    echo ”;
    foreach ($cols as $col){
    echo ”.$col.”;
    }
    echo ”;

    $query=mysql_query(‘select * from `’.$table.”,$db);
    while ($row=mysql_fetch_assoc($query)){
    echo ”;
    foreach ($cols as $col){
    echo ”.$row[$col].”;
    }
    echo ”;
    }
    echo ”;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">