How to Get Data From Database in PHP Using Id

In this post, i will show you step by step on how to get/fetch data from MySQL database in PHP using id. And as well as i will show you how to display this data on html page.

In this example, I will use latest bootstrap 4 library to display fetched data from MySQL database with PHP on html. As well as will use easy mysqli function (mysqli_query(), mysqli_connect(), mysqli_fetch_row()) and sql query to get single row data from MySQL DB using id and display it on HTML web page in PHP.

Note that – You must have installed XAMPP server on your window, ubuntu or Mac machine. As well as any text editor installed, which can be used to write code in your php files.

Retrieve Data From Database in PHP Using Id

You can follow below given simple steps for retrieve data from database in php using id and display on html web page:

Step 1 – Create Project Folder

In step 1, navigate to local web server directory. And inside directory, create one folder. And you can name this folder anything.

For example, i will use xampp server so, i willl navigate to C/xampp/htdocs/.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Run SQL query to Create Table

In step 2, open your browser and type http://localhost/phpmyadmin. Then create database and table by running the following sql query:


CREATE TABLE `customers` (
PRIMARY KEY (`custId`)

INSERT INTO `customers` (`custId`, `fname`, `lname`, `email`, `created`) VALUES (NULL, 'Tiago', 'Sam', '[email protected]', NULL), (NULL, 'Anil', 'Kumar', '[email protected]', NULL);

Step 3 – Create phpmyadmin MySQL Database Connection File

In step 3, create a php file that named mydbCon.php. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create mydbCon.php file and add the below given code into your file:


    $hName='localhost'; // host name

    $uName='root';   // database user name

    $password='';   // database password

    $dbName = "my_db"; // database name

    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");

          die('Could not Connect MySql Server:' .mysql_error());

Step 4 – Get Single Row From Database in PHP using Id

In step 4, you need to create a php file that named get-data-by-id.php. Which is used to get single row data from MySQL database table in PHP using id.

So add the following code on get-data-by-id.php file:


include 'mydbCon.php';

$sql = "SELECT * FROM customers WHERE custId='" . $_GET["custId"] . "'"; // Fetch data from the table customers using id


$singleRow = mysqli_fetch_assoc($result);


Step 5 – Create Html Page To Display Single Record

In step 5, you need create a php file that named index.php. Which is used to display single row data from the MySQL database table.

Note that, you need to include get-data-by-id.php file on index.php file. Don’t worry i have already include this file on index.php file. Because this get-data-by-id.php file code will get single row data from database table in php using id.

So, you can add this code in your index.php file:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>How to Get Data From Database in PHP Using Id</title>
    <link rel="stylesheet" href="">

<div class="container mt-2">
    <div class="row">
        <div class="col-md-12">
            <?php include 'get-data-by-id.php '; ?>
            <div class="card">
              <div class="card-header">
               Get Data From Database in PHP Using Id
              <div class="card-body">
                <b>First Name</b> :- <span class="card-text"><?php echo $singleRow['fname']; ?> </span><br>
                <b>Last Name</b> :- <span class="card-text"><?php echo $singleRow['lname']; ?></span><br>
                <b>Email Name</b> :- <span class="card-text"> <?php echo $singleRow['email']; ?></span><br>


Step 6 – Open Browser And Run This Project

Finally, you need to open your browser and type http://localhost/demo/?custId=1 this run project on local machine.

How to get and display single row data in php using id will be looks like shown in below image:

Thanks for reading this post.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *