Skip to content

Instantly share code, notes, and snippets.

@ohaal
Created May 24, 2016 20:33
Show Gist options
  • Save ohaal/13c0ffee81653e725282184ba6dcaa53 to your computer and use it in GitHub Desktop.
Save ohaal/13c0ffee81653e725282184ba6dcaa53 to your computer and use it in GitHub Desktop.
Simple MySQL query to HTML
function queryToHtml($mysqli, $query) {
if ($result = $mysqli->query($query)) {
/* Get field information for all columns */
$finfo = $result->fetch_fields();
// Print headers
print('<table><tr>');
foreach ($finfo as $val) {
printf('<th>%s</th>', $val->name);
}
print('</tr>');
// Print row contents
while ($row = $result->fetch_row()) {
print('<tr>');
foreach ($row as $col) {
printf('<td>%s</td>', $col);
}
print('</tr>');
}
print('</table>');
$result->free();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment