Get MySQL Field Names From Query with PHP

Print a dynamic table with fields names in the header from any MySQL query.

Get MySQL Field Names From Query with PHP

A php script to display dynamically the fields name for any MySQL query with PHP and show them in a table header, then it display resultsets in table rows.
You  can just change the SQL query to display the results you want with field names in the table header.

$conn = mysql_connect("localhost", "user", "pass");

if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('your_db_name');

// Change query to your convenience
$sql="SELECT * FROM user";

print '<table width="90%" cellpadding="3" cellspacing="1" border="1">';
if ($result=mysql_query($sql)) {
    // Print fields names
    print '<tr>';
    $i = 0;

while ($i < mysql_num_fields($result)) {
        $meta = mysql_fetch_field($result, $i);
        print '<th>'.$meta->name.'</th>';
        $i++;
   }
    print '</tr>';
   // Print data
    while($row=mysql_fetch_assoc($result)) {
        print '<tr>';
        foreach ($row as $key => $value) {
            print '<td>'. $value.'</td>';
        }
        print '<tr>';
    }
}
print '</table>';