function array2table: small fix to the post below that handles data returned from mysql that is either null or 0...
This is a useful script for displaying MySQL results in an HTML table.
function array2table($arr,$width)
{
$count = count($arr);
if($count > 0){
reset($arr);
$num = count(current($arr));
echo "
echo "
\n";foreach(current($arr) as $key => $value){
echo "
";echo $key." ";
echo "
\n";}
echo "
\n";while ($curr_row = current($arr)) {
echo "
\n";$col = 1;
while (false !== ($curr_field = current($curr_row))) {
echo "
";echo $curr_field." ";
echo "
\n";next($curr_row);
$col++;
}
while($col <= $num){
echo "
\n";$col++;
}
echo "
\n";next($arr);
}
echo "
\n";}
}
?>
// Add DB connection script here
$query = "SELECT * FROM mytable";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$array[] = $row; }
array2table($array,600); // Will output a table of 600px width
?>