Output result set from MySQL in a html table in PHP
Description
The following code shows how to output result set from MySQL in a html table.
Example
<?php// ww w.j a va 2s . c o m
// Connect to the MySQL database
$mysqli = new mysqli("localhost", "root", "", "test");
// Create the query
$query = "SELECT * FROM employee";
// Execute the query
if ($result = $mysqli->query($query)) {
printf("<table border='1'>");
printf("<tr>");
// Output the headers
$fields = $result->fetch_fields();
foreach ($fields as $field)
printf("<th>%s</th>", $field->name);
printf("</tr>");
// Output the results
while ($employee = $result->fetch_row()) {
$first_name = $employee[0];
$last_name = $employee[1];
$email = $employee[2];
$phone = $employee[3];
// Format the phone number
$phone = ereg_replace("([0-9]{3})([0-9]{3})([0-9]{4})",
"(\\1) \\2-\\3", $phone);
printf("<tr>");
printf("<td>%s</td><td>%s</td>", $first_name, $last_name);
printf("<td>%s</td><td>%s</td>", $email, $phone);
printf("</tr>");
}
}
?>