How to Fetch Single Row Data From Database in PHP

How to fetch single row data from database and display on html page in php post, i will teach you step by step on how to fetch/retrieve single row data from mysql database in php and display in html page.

Also, I will latest bootstrap 4 library to display fetched data from MySQL database with PHP on html. As well as i will use easy mysqli function (mysqli_query(), mysqli_connect(), mysqli_fetch_row()) to retrieve single row data from MySQL DB and display it on HTML web page.

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.

Fetch Single Row Data/Record From Database in PHP MySQL

Let’s follow the below given steps to fetch single row data from database in php mysqli and display on html web page table:

  • Step 1 – Create PHP Project
  • Step 2 – Execute SQL query to Create Table
  • Step 3 – Create phpmyadmin MySQL Database Connection File
  • Step 4 – Fetch Single Row From Database in PHP
  • Step 5 – Create Html Page To Display Fetched Data From MySQL DB
  • 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 Single Row From Database in PHP

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

So add the following code on fetch-single-row-data.php file:

<?php


include 'mydbCon.php';

$query="select * from customers limit 1"; // Fetch all the data from the table customers

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

$singleRow = mysqli_fetch_row($result);

?>

Step 5 – Create Html Page To Display Fetched Data From MySQL DB

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 fetch-single-row-data.php file on index.php file. Don’t worry i have already include this file on index.php file. Because this fetch-single-row-data.php file code will fetch single row data from database table.

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Fetch Single Row Data From Database in PHP</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 'fetch-single-row-data.php'; ?>
            <div class="card">
              <div class="card-header">
                Fetch Single Row Data From Database in PHP and Display in HTML Page
              </div>
              <div class="card-body">
                <b>First Name</b> :- <span class="card-text"><?php echo $singleRow['1']; ?> </span><br>
                <b>Last Name</b> :- <span class="card-text"><?php echo $singleRow['1']; ?> </span><br>
                <b>Email Name</b> :- <span class="card-text"> <?php echo $singleRow['1']; ?></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 this run project on local machine.

Recommended PHP Tutorials

Thanks for reading this post.

Leave a Comment