display table from database in php code example
Example 1: showing database table in php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('hrmwaitrose');
$query = "SELECT * FROM employee";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>";
}
echo "</table>";
mysql_close();
Example 2: how to display data from mysql database into html table using php
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>