how to fetch data from database in php and display in html table

how to fetch data from database in php and display in html table

In this tutorial, we will see how to fetch data from the database in PHP and display in html table. After this tutorial, you will be able to how to connect databases in PHP and fetch data. Here we will discuss with an example step by step. You will understand how to fetch data from the database in PHP and display in html table. If you have any questions about database connection and data fetching and showing here I will provide you with the best example with solutions of your questions. After that, you will be able to connect the database and fetch data in PHP.

Data management is an essential aspect of web development, and the ability to fetch data from a database and display it in a user-friendly format is a critical skill for any web developer. In this tutorial, we will show you how to fetch data from a database in PHP and display it in an HTML table.

Before diving into the steps, it is important to have a basic understanding of PHP and HTML. You will also need access to a database management system, such as MySQL, to follow along with the tutorial.

Step 1: Creating a file for database connection:

<?php 
$conn= mysqli_connect(“localhost”, “root”,””,”database_name”);
If(!$conn){
Alert(‘Database connection error’);
)?>

 

Step 2: Fetching data to display

<?php
include_once 'db.php';

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>how to fetch data from database in PHP and display in html table</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
 
</head>

<body>
    <div class="bs-example">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header clearfix">
                        <h2 class="pull-left">Posts List</h2>
                    </div>
                    <?php
                    $result = mysqli_query($conn, 'SELECT * FROM posts');
                    ?>
                    <?php
                        if (mysqli_num_rows($result) > 0) {
                    ?>
                    <table class='table table-bordered table-striped'>
                        <tr>
                            <td>ID</td>
                            <td>Post Title</td>
                            <td>Post Auther</td>
                            <td>Category</td>
                            <td>Content</td>
                        </tr>
                        <?php
                            $i=0;
                            while($row = mysqli_fetch_array($result)) {
                        ?>
                        <tr>
                            <td><?php echo $row['id']; ?></td>
                            <td><?php echo $row['title']; ?></td>
                            <td><?php echo $row['author']; ?></td>
                            <td><?php echo $row['category']; ?></td>
                            <td><?php echo $row['content']; ?></td>
                        </tr>
                        <?php
                            $i++;
                            }
                        ?>
                    </table>
                    <?php
                        }else{
                            echo "No records fund!";
                        }
                    ?>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

I hope this tutorial is helpful for you.