Skip to content

All topics  /  PHP

How to Fetch And Display Data From The Database In PHP?

PHP ·

Teams applying “How to Fetch And Display Data From The Database In PHP?” in a production project can use the official resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

Hi Dev,

This tutorial is focused on how to fetch and display data from the database in PHP. you'll learn how to fetch and display data from a database using PHP. We will use how to fetch and display the data from the MySQL database in PHP. if you have a question about how to fetch and display the data from the MySQL database using PHP then I will give a simple example with a solution.

Now, let's see the article on how to fetch and display data from the MySQL database using PHP. it's a simple example of how to fetch and display data from the MySQL database in PHP. you can understand the concept of how to fetch and display data from a database using PHP. if you have a question about how to fetch and display data from the database in PHP then I will give a simple example with a solution.

Step 1: Create a Database Connection File


<?php
    $servername='localhost';
    $username='root';
    $password='';
    $dbname = "my_db";

    
    $conn=mysqli_connect($servername,$username,$password,$dbname);

      
    if(!$conn){
        die('Could not Connect MySql Server:' .mysql_error());
    }
?>

Step 2: Fetch data from the database and display in table

<!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>Bootstrap 4 Bordered Table</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round|Open+Sans">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <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>
    <style type="text/css">
        .bs-example{
            margin: 20px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();   
        });
    </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">Users List</h2>
                    </div>
                    <?php
                        include_once 'db.php';
                        $result = mysqli_query($conn,"SELECT * FROM users");
                    ?>
                    <?php
                        if (mysqli_num_rows($result) > 0) {
                    ?>
                    <table class='table table-bordered table-striped'>
                        <tr>
                            <td>Name</td>
                            <td>Email id</td>
                            <td>Mobile</td>
                        </tr>
                        <?php
                            $i=0;
                            while($row = mysqli_fetch_array($result)) {
                        ?>
                        <tr>
                            <td><?php echo $row["name"]; ?></td>
                            <td><?php echo $row["email"]; ?></td>
                            <td><?php echo $row["mobile"]; ?></td>
                        </tr>
                        <?php
                            $i++;
                            }
                        ?>
                    </table>
                    <?php
                        }else{
                            echo "No result found";
                        }
                    ?>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>

I hope it could help you...