I am trying to display a whole mysql table in an html table. So far I have found the below code which works to display fields:
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if(mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "
Firstname | Lastname |
---|
while($row = mysqli_fetch_array($result)) {
echo "
";echo "
" . $row['FirstName'] . "";echo "
" . $row['LastName'] . "";echo "
";}
echo "
";mysqli_close($con);
?>
Is there a way that I can display all columns/rows without having to type all the column names as
echo "
" . $row['FirstName'] . "";解决方案
You can do it if you are going to show all columns. Or, you can add your filters conditional.
However, as it seems you are new to web development, I make it in a simple way.
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "
";foreach ($row as $key => $value) {
echo "
" . $key . "";}
echo "
";}
echo "
";foreach ($row as $value) {
echo "
" . $value . "";}
echo "
";}
echo "
";mysqli_close($con);
?>