Newsletter:

(Code) Displaying Fields and Data of a MySql Table in PHP

Code : Displaying Fields and Data of a MySql Table in PHP

We often have to view MySQL tables in a tabular form without the SQL query console. Sometimes you have to see what all fields are there in a MySQL table and what information they contain. The following piece of code presents to you a tabular format of your table and its contents.

<?php
$query="select * from table_name";
$result=mysql_query($query);

echo "<table border=1>";
echo "<tr>";
for($i=0;$i<mysql_num_fields($result);$i++)
{

    echo "<th>";
    echo mysql_field_name($result, $i);
    echo "</th>";
}


while($row=mysql_fetch_row($result))
{
    echo "<tr>";
    for($j=0;$j <$i;$j++)
    {
    echo "<td>";
    echo $row[$j];
    echo "</td>";
    }
    echo "</tr>";
}

echo "</tr>";
echo "</table>";

?>

About the Author
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at amrit(at)bytesworth.com. For further details, visit http://www.bytesworth.com