PHP - Fetch Data From MySql Database

Published June 26, 2021

In this PHP example we will learn how to write a php file to fetch data from mysql database table with PHP query.

This example we will create two php script files one is for Create Database connection and queries and other one is for parse the MySql query response data and display data on the UI.


View Demo


Databse Connection

    define('DB_PASS' ,'');
    define('DB_NAME', 'ebridge');
    class DB_con
    function __construct()
    $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
    // Check connection
    if (mysqli_connect_errno())
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    public function get()
    $ret=mysqli_query($this->dbh,"select * from userdata");
    return $ret;


Parse MySql response on display on UI

<!DOCTYPE html>
<html lang="en">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>RRTutors | DEMO</title>
        <meta name="generator" content="Bootply" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link href="" rel="stylesheet">
        <!--[if lt IE 9]>
            <script src="//"></script>
        <link href="" rel="stylesheet">
                table {
                font-family: Arial, Helvetica, sans-serif;
                border-collapse: collapse;
                width: 100%;

                td, th {
                border: 1px solid #ddd;
                padding: 8px;

                tr:nth-child(even){background-color: #f2f2f2;}

                tr:hover {background-color: #ddd;}

                th {
                padding-top: 12px;
                padding-bottom: 12px;
                text-align: left;
                background-color: #04AA6D;
                color: white;
        <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="navbar-header">
                <a class="navbar-brand" rel="home" href="">RRTutors</a>
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>


        <div class="container-fluid">


            <div class="col-sm-6">
                <div class="row">
                    <div class="col-xs-12">
                        <h3>Fetch Data from Database using PHP MySql</h3>

                            $insertdata=new DB_con();


                                while ($data = $sql->fetch_assoc())
                                ?>    <tr>
                            <td><?php  echo $data['id'];?> </td>
                            <td><?php  echo $data['name'];?> </td>
                            <td><?php  echo $data['email'];?> </td>
                            <td><?php  echo $data['addrss'];?> </td>




            <div class="col-sm-6">

        <!-- script references -->
        <script src="//"></script>
        <script src=""></script>

You can find the complete source code below




Article Contributed By :