I am trying to build a function that extracts information from a database and inserts it into an associative array in PHP using mysql_fetch_assoc
, and return the ar
The following should work:
$rows = mysql_query("select * from whatever");
if ($rows) {
$header = true;
while ($record = mysql_fetch_assoc($rows)) {
if ($header) {
echo '';
foreach (array_keys($record) AS $col) {
echo ''.htmlspecialchars($col).' ';
}
echo ' ';
$header = false;
}
echo '';
foreach (array_values($record) AS $col) {
echo ''.htmlspecialchars($col).' ';
}
echo ' ';
}
}
(Yes, blatant mod of Fosco's code)
This should print the column headers once, then the contents after that. This would print just whatever columns were retrieved from the DB, regardless of the query.