How to fetch data from database

Project : in this project we will learn how to fetch data from database and display in table.

We will display all the data in table on user-list.php


index.php

Add data : To fetch all the data first of all we have to insert data in database table that we have done in previuos lession.

myproject/index.php
index.php
PHP
How to Insert data in database Click here

List of Users

myproject/user-list.php
user-list.php
                
                <?php include("fetch.php"); ?>
                <?php include("common/header.php"); ?>
                <body>
                  <!-- Empty Table -->
                    <div class="container-fluid" style="margin-top: 50px;">
                        <div class="container w-75">
                          <h3>User List</h3>
                            <table class="table table-striped">
                              <thead class="bg-dark text-white"> 
                                <tr> 
                                  <th>Id </th>
                                  <th>First name</th>
                                  <th>Last Name </th>
                                  <th>Username </th>
                                  <th>email </th>
                                  <th>Action </th>
                              </tr>
                            </thead>
                              <tbody>
                                <tr>
                                  <td> ......... </td>
                                  <td> ......... </td>
                                  <td> ......... </td>
                                  <td> ......... <td>
                                  <td> ......... </td>
                                  <td>
                                    <a class="btn btn-info" href="#">View </a>
                                    <a class="btn btn-success" href="#">Edit </a>
                                    <a class="btn btn-danger" href="#" >Delete </a>  
                                  </td>
                              </tr>
                            </tbody>    
                          </table>       
                        </div>
                      </div>
                  <?php include("common/footer.php"); ?>
                
              

Database Connection

Database-> dbdemo Table-> users
Create connection with database
myproject/dbcon.php
dbcon.php
    
        $dbhost ="localhost";
        $dbuser ="root";
        $dbpswd ="";
        $dbname ="dbdemo";
        $dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
        if($dbcon){
            //echo "Connected............!!!";
            return $dbcon;
            exit();
        }else{
            die("Connection failed ? ? ?". mysqli_error($dbcon));
        }
        mysqli_close($dbcon);
    

Fetch Data

Process for fetching data from database
myproject/fetch.php
fetch.php
                
                  <?php include("dbcon.php"); //This is database connection file
                    $success=''; $error='';
                    $sql = "SELECT * FROM users";
                    $run = mysqli_query($dbcon,$sql);
                    //we have to include this file on index.php and implement all variable inside table
                    mysqli_close($dbcon);
                
            
Display data in table (Ouput)
Implement variable in user-list.php
myproject/user-list.php
user-list.php
            
              <?php include("fetch.php"); ?>
                <?php include("common/header.php"); ?>
                  <body>
                    <div class="container-fluid" style="margin-top: 50px;">
                      <div class="text-center"> <?php echo $success; ?>  <?php echo $error; ?> </div>
                      <?php 
                        //  Message will be displayed when user will be deleted
                          if(isset($_GET['1'])){
                              echo $_GET['1'];
                              header( "refresh:2;url=index.php" );
                          } 
                          if(isset($_GET['0'])){
                              echo $_GET['0'];
                              header( "refresh:2;url=index.php" );
                          } 

                        ?> 
                        <div class="container w-75">
                          <h3>User List</h3>
                            <table class="table table-striped">
                              <thead class="bg-dark text-white"> 
                                <tr> 
                                  <th>Id </th>
                                  <th>First name</th>
                                  <th>Last Name </th>
                                  <th>Username </th>
                                  <th>email </th>
                                  <th colspan="2">Action </th>
                              </tr>
                            </thead>
                            <tbody>
                            <?php while($data = mysqli_fetch_array($run)){ ?> <!-- $run  variable coming from fetch.php -->
                              <tr>
                                <td> <?php echo $data['id']; ?> </td>  <!-- the index value ['id'] is from database table -->
                                <td> <?php echo $data['fname']; ?> </td> <!-- the index value ['fname'] is from database -->
                                <td> <?php echo $data['lname']; ?> </td> <!-- the index value ['']fname is from database -->
                                <td> <?php echo $data['username']; ?> </td> <!-- the index value ['ussername'] is from database -->
                                <td> <?php echo $data['email']; ?> </td> <!-- the index value ['email'] is from database -->
                                <td>
                                    <!-- This part is for view, update and delete for single data  -->
                                  <a class="btn btn-info " href="view.php?view_id=<?php echo $data['id']; ?>">View</a>
                                  <a class="btn btn-success" href="update-form.php?edit_id=<?php echo $data['id']; ?>">Edit</a>
                                  <a class="btn btn-danger" href="delete.php?del_id=<?php echo $data['id']; ?>">Delete</a>
                                <td>
                             <tr>
                            <?php  }   ?>
                            <tbody>    
                          </table>       
                        </div>
                      </div>
                  <?php include("common/footer.php"); ?>
              
            
Check image below Display data in table (Output)
PHP

common

header.php
    
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1,
            shrink-to-fit=no">
            <title>Codetechinfo</title>
            <link rel="stylesheet" href="css/bootstrap.min.css">
        </head>
    
navbar.php
    
        <nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
            <a class="navbar-brand text-white" href="./index.php">Learn PHP  </a>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link text-white" href="./index.php">Home  </a>
                    </li> 
                </ul>
            </div>
        </nav>
        <br><br>
    
footer.php
    
            <div class="copyright py-4 text-center text-white">
                <div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
            </div>
            </body>
        </html>
    

css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css