How to fetch Single data from database

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

First We have to fetch all data and display in table on user-list.php then from user-list, we will fetch single data by clicking on view button.


index

Add data : Add data in database table.
myproject/index.php
index.php (add data)
PHP
How to insert data in database Click here

Fetch all data List of users

Fetch All : We have to fetch all the data from data that we have previous lession.
myproject/user-list.php
user-list.php
PHP
How to fetch all data in database Click here

How to fetch data from database

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

myproject/view.php
view.php
              
                <?php include("fetch-single.php"); ?>
                <?php include("common/header.php"); ?>
                <body>
                <!-- Empty Table -->
                    <div class="container-fluid" style="margin-top: 50px;">
                      <div class="text-center"> </div>
                        <div class="container w-50">
                          <h3>Single Data View </h3>
                          <ul class="list-group">
                              <li class="list-group-item active">Data for single User <b></b> </li>
                              <li class="list-group-item">ID :  <b>..................... </b> </li>
                              <li class="list-group-item">First Name : <b>...............</b> </li>
                              <li class="list-group-item">Last Name :  <b>...............</b> </li>
                              <li class="list-group-item">username :  <b>................</b> </li>
                              <li class="list-group-item">Email : <b>................... </b> </li>
                          </ul>
                        </div>
                      </div>
                  <?php include("common/footer.php"); ?>
               
            

Data in table

We have already fetched all data in previous lession, displayed in the table.

Now we have to fetch single user data from database by click on view button.

PHP

<a href="view.php?view_id=<?php echo $data['id']; ?>">View</a>
query string : view_id=<?php echo $data['id']; ?> // to get id using $_GET method

PHP

Recieve view_id on fetch-single.php (follow as in image below).

PHP

Database Connection

Database exists: 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-signle.php
fetch-single.php
                
                  <?php include("dbcon.php"); //This is database connection file
                  $error=''; $success='';
                  if(isset($_GET['view_id'])){
                      $view_id = $_GET['view_id'];
                      $query = "SELECT * FROM users WHERE id ='$view_id'";
                      $run = mysqli_query($dbcon,$query);
                      if(mysqli_num_rows($run) > 0){
                        $data = mysqli_fetch_assoc($run);
                        $view_id = $data['id'];
                        $view_fname = $data['fname'];
                        $view_lname = $data['lname'];
                        $view_username = $data['username'];
                        $view_email = $data['email'];
                      }else{
                      $error = "No data found ";
                      }
                  }else{
                    $error = "No data found ";
                  }
                  mysqli_close($dbcon);
                
            
Data in table (Output)

Display data in table

myproject/view.php
view.php
            
                <?php include("fetch-single.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>
                        <div class="container w-50">
                          <h3>Single Data View </h3>
                          <ul class="list-group">
                              <li class="list-group-item active">Data for single User </li>
                              <li class="list-group-item">ID :  </b> <?php echo  $view_id; ?> <b> </li>
                              <li class="list-group-item">First Name : </b> <?php echo  $view_fname; ?> <b> </li>
                              <li class="list-group-item">Last Name :  </b> <?php echo  $view_lname; ?><b> </li>
                              <li class="list-group-item">username :  </b> <?php echo  $view_username; ?><b> </li>
                              <li class="list-group-item">Email : </b> <?php echo  $view_email; ?> <b> </li>
                          </ul>
                        </div>
                      </div>
                  <?php include("common/footer.php"); ?>
              
            
Check image below Display data in list (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