PHP Tutorial: Print All MySQL Database Tables & Fields

Comments 17 Standard

* File: displaytables.php
* Date: 1.13.2009
* Author:, LLC
* Purpose: display all table structure for a specific database

//connection variables
$host = "localhost";
$database = "your_db_name";
$user = "your_username";
$pass = "your_pass";

//connection to the database
mysql_connect($host, $user, $pass)
or die ('cannot connect to the database: ' . mysql_error());

//select the database
or die ('cannot select database: ' . mysql_error());

//loop to show all the tables and fields
$loop = mysql_query("SHOW tables FROM $database")
or die ('cannot select tables');

while($table = mysql_fetch_array($loop))

    echo "
        <table cellpadding=\"2\" cellspacing=\"2\" border=\"0\" width=\"75%\">
            <tr bgcolor=\"#666666\">
                <td colspan=\"5\" align=\"center\"><b><font color=\"#FFFFFF\">" . $table[0] . "</font></td>

    $i = 0; //row counter
    $row = mysql_query("SHOW columns FROM " . $table[0])
    or die ('cannot select table fields');

    while ($col = mysql_fetch_array($row))
        echo "<tr";

        if ($i % 2 == 0)
            echo " bgcolor=\"#CCCCCC\"";

        echo ">
            <td>" . $col[0] . "</td>
            <td>" . $col[1] . "</td>
            <td>" . $col[2] . "</td>
            <td>" . $col[3] . "</td>
            <td>" . $col[4] . "</td>

    } //end row loop

    echo "</table><br/><br/>";
} //end table loop

17 thoughts on “PHP Tutorial: Print All MySQL Database Tables & Fields

  1. Hi, Nice one. thanks. And with your permission i can get this details from my site. And give your reference link for this url.

    Are you giving permission to do this. I have no more english language.


  2. There is an additional column that can be shown. The updated listing is shown here.

    : show table structure :
    +………………………………………..+ */
    $database = “test_db”;
    $loop = mysqli_query($db, “SHOW tables FROM $database”) or die (‘cannot select tables’);

    while($table = mysqli_fetch_array( $loop)) {
    echo ”

    ” . $table[0] . ” table structure

    Type : Length

    $i = 0; //row counter
    $row = mysqli_query($db, “SHOW columns FROM ” . $table[0])
    or die (‘cannot select table fields’);

    while ($col = mysqli_fetch_array($row)) {
    echo ”
    ” . $col[0] . ”
    ” . $col[1] . ”
    ” . $col[2] . ”
    ” . $col[3] . ”
    ” . $col[4] . ”
    ” . $col[5] . ”

    } //end row loop

    echo “”;
    } //end table loop

  3. hi,
    I was just wandering,
    how many columns can we add?
    how does the loop knows from $col ?

    thx in adb=vance,

  4. You can add up to count($col) columns to the list. If you always want to print all of them you can change the code inside of the while ($col = mysql_fetch_array($row)) to the following:

    while ($col = mysql_fetch_array($row))
    echo "<tr>";

    //print the whole array returned in $col
    for ($j = 0; $j < count($col); $j++)
    echo "<td>{$col[$j]}</td>";

    echo "</tr>";

    } //end row loop

  5. That’s a great, cute script ! No need to go into cPanel / phpadmin anymore ! But now; Do you have any sample to print certain db-field contents just like an address book, such as this ?:

    Name Surname e-mail address
    Peter Baker 0123 45678 peter&

    The script that I “created” prints it all, but not formatted, with certain distance between the fields. I don’t find a way how to format the headline and then underneath the content of the db equally. Sorry, PHP knows me perhaps, but I don’t know much about PHP….

    Thanks for any advise,if you have

  6. Hola! I’ve been reading your web site for a long time now and finally got the bravery to go ahead and give
    you a shout out from Atascocita Texas! Just wanted to tell you keep up the good work!

  7. Hi I am absolutely loving this script it is very close to a small personal project I have been work on,
    it is also the first script when I have copied it into one of my test pages it actually works instead of setting the HTTP error 500,
    it feels fitting I end up here as your site is a lot bout game design,
    I am trying to learn more in PHP I own my own domain and I provide a sub domain to a online gaming community its a group of volunteers that host live online back gammon and cribbage tournaments,
    so I am attempting to create a tool for them as to score keep on multiple mini tournaments at once,
    so I need to have a text type input box to add a player name (varchar) once submitted this needs to also set them a score of 0 (this section even I can setup) then I need to be able to output the list of names and relative scores into a nice table with something like a check box to the right corresponding to each row,
    when checked 1 point is added to that record
    finished off with a page refresh to update the scores in the table
    I know I don’t ask for much but I feel as I am banging my head against a solid wall trying all the ways I can find online

  8. Hi Michael,

    Thanks for your comment. I have some great tutorials about setting up a database and then using a form to add records to the database in my Pits of Doom game tutorial. It also teaches you how to do incremental development on a game as you’re learning to program at the same time. Hopefully that will be a big help for you if you follow the tutorials and it’s open source so you can use any of the code in your own project. Best of luck to you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s