How to Fetch and Display Data From Database in PHP by Id

How to fetch and display data from database in php using id; 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 or Fetch Data From Database in PHP By Id

  • Step 1 – Create PHP Project
  • Step 2 – Execute SQL query to Create Table
  • Step 3 – Create phpmyadmin MySQL Database Connection File
  • Step 4 -Fetch Data From Database in PHP using Id
  • Step 5 – Create Html Page To Display Fetch Record
  • Step 6 – Open Browser And Test This Project

Step 1 – Create PHP Project

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 – Execute 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 DATABASE my_db;


CREATE TABLE `customers` (
`custId` INT(11) NOT NULL AUTO_INCREMENT,
`fname` VARCHAR(255) NULL DEFAULT NULL,
`lname` VARCHAR(255) NULL DEFAULT NULL,
`email` VARCHAR(255) NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`custId`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1;

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:

<?php

    $hName='localhost'; // host name

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

    $password='';   // database password

    $dbName = "my_db"; // database name

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

      if(!$dbCon){
          die('Could not Connect MySql Server:' .mysql_error());
      }
?>

Step 4 -Fetch Data 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:

<?php


include 'mydbCon.php';

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

$result=mysqli_query($dbCon,$sql);

$singleRow = mysqli_fetch_assoc($result);


?>

Step 5 – Create Html Page To Display Fetch 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">
<head>
    <meta charset="UTF-8">
    <title>How to Get Data From Database in PHP Using Id</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<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>
              <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>
              </div>
            </div>
        </div>
    </div>        
</div>

</body>
</html>

Step 6 – Open Browser And Test 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:

Recommended PHP Tutorials

Thanks for reading this post.

Be First to Comment

Leave a Reply

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