Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How to display data from MySQL database to HTML table using PHP?

 


index.php

<DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css.css">
<meta charset="utf-8">
  <meta name="viewport" content="width=device-width,
initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3
/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3
/dist/js/bootstrap.bundle.min.js"></script>
</head>
<title>Hancie e-Learning Studio</title>
<body>
<div class="container">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>DOB</th>
</tr>
</thead>

<?php
include ('connection.php');

$sql="SELECT * FROM bio";

$result=mysqli_query($conn, $sql);

if($result->num_rows>0){


while($row=$result->fetch_assoc()){
echo "<tr><td>" .$row['ID']. "</td><td>"
.$row['Name'] ."</td><td>"
.$row['Age'] ."</td><td>"
.$row['DOB'] ."</td></tr>";
}
}
else {
  echo "No result";
}

?>
</table>
</div>

</body>

</html>

connection.php


<?php

$servername="localhost";
$username="root";
$password="";
$dbname="practice";

$conn=new mysqli($servername, $username, $password, $dbname);

if(!$conn){
    die ("Could not connect" .mysqli_error());
}





?>

AnotherMethod.php

<table>
<tr>
    <th>ID</th>
    <th>Name</th>
    <th>Age</th>
    <th>DOB</th>
</tr>
<?php
include ('connection.php');

$sql="SELECT * FROM bio";

$query=mysqli_query($conn, $sql);

while ($row=mysqli_fetch_array($query)) {
    ?>

    <tr>

    <td><?php echo $row['ID']?></td>
    <td><?php echo $row['Name']?></td>
    <td><?php echo $row['Age']?></td>
    <td><?php echo $row['DOB']?></td>


    </tr>
<?php
}
?>
</table>



Output









Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.