<?php
//connect to server and choose database
$conn=mysql_connect("localhost", "user", "password");
$db=mysql_select_db("db_name", $conn);
//run query and hold result identifier in variable $result
$result=mysql_query("SELECT * FROM table_name");
//the first time through I want to print the column names
//in the table header. This variable will be changed to
//FALSE once the table header is laid out
$layout_header=TRUE;
echo "<table border=1>\n";
//This while... loop will grab all results from the query.
//Using MYSQL_ASSOC, the column names are stored as array
//associative indexes.
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
if($layout_header){
$layout_header=FALSE;
echo "<tr>\n";
//print out each column name as the value of a row in the
//table header
while(list($col_name, $col_value)=each($row)){
echo "<th>$col_name</th>\n";
}
//rewind the array pointer so that the values of this
//row can be displayed in the table body.
reset($row);
echo "<tr>\n";
}
//now that the table header is in place, print
//all the values for each row.
echo "<tr>\n";
while(list($col_name, $col_value)=each($row)){
echo "<td>$col_value</td>\n";
}
echo "<tr>\n";
}
echo "</table>";
?>