PHP program to fetch data from database and display in a HTML table

Write a PHP program to fetch data from database and display in a HTML table


In this tutorial, you will learn how to fetch data from database and display in a HTML table in php, so to read customer information like Cust_no, Cust_name, Item_purchased and Mob_no. from Customer table and display all these information in table format on output screen.

Below is the php script to fetch data from database and display in a HTML table in php

<html>
<head>
    <title>PHP program to fetch data from database in & show in html table</title>
</head>
<body>

    <h3>Write a PHP program to fetch data from database and display in a HTML table in php</h3>

    <table border="2" bordercolor="blue" bgcolor="#00FF">
        <thead>
            <tr>
                <th>Cust_Id</th>
                <th>Cust_Name</th>
                <th>Items_Purchased</th>
                <th>Phone_No</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $connection = mysqli_connect("localhost","root","");
                $db = mysqli_select_db($connection,"database_name");

                $query = mysqli_query($connection,"SELECT * FROM customer");

                if (mysqli_num_rows($query)==0)
                {
                    echo "<tr>
                            <td colspan='4'> No rows returned </td>
                        </tr>";
                }
                else
                {
                    while($row=mysqli_fetch_row($query))
                    {
                        echo "<tr>
                                <td> $row[0] </td>
                                <td> $row[1] </td>
                                <td> $row[2] </td>
                                <td> $row[3] </td>
                            </tr> ";
                    }
                }
            ?>
        </tbody>
    </table>

</body>
</html>


Thanks for reading.

© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT