All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Get Data From Database In PHP Using Id

Last Updated : Mar 11, 2024

How To Get Data From Database In PHP Using Id

In this tutorial we will show you the solution of how to get data from database in PHP using id, here we defined form with input fields for collecting user id and another one is submit button then we defined bootstrap card for displaying retrieved id results.

When user clicks submit in php we executes database connection and collecting user entered id and we checks in table data where id matched then respective database result will fetched and displayed on webpage.

Step By Step Guide On How To Get Data From Database In PHP Using Id :-

Here for getting input and showing result on webpage in html block we defined form with input fields these are designed by bootstrap so we imported bootstrap css cdn link.

After getting user id when user clicks submit button in php we executes once database ‘dbase’ connection then we checks is submit button is set or not.

If they sets then we collecting user entered id value by ‘post’ method and stored to variable ‘$uid’.

We defining select query with condition to checks in table ‘info’ which row match with user entered id value then we executed query with database and stored to variable $result.

For fetching respective row from table we used ‘mysqli_fetch_assoc()’ method and stored to variable $single_row.

In html block we using this variable to printed matched row ‘user name, password’ on webpage.

include 'dbcon.php';
$sql = "SELECT * FROM info WHERE ID='" . $uid . "'";
$singleRow = mysqli_fetch_assoc($result);
<!DOCTYPE html>
<title>Get Data From Database Using Id</title>
<link rel="stylesheet" href="">
    <form method="post" class="text-center p-4">
        <input type="text" name="uid" placeholder="Enter id">
        <input type="submit" name="submit" value="submit">
<div class="container mt-2">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
Get Data From Database in PHP Using Id
<div class="card-body">
<b>Name</b> :- <span class="card-text"><?php echo $singleRow['Name']; ?></span><br>
<b>Password</b> :- <span class="card-text"><?php echo $singleRow['Password']; ?></span><br>


    $dbName = "dbase";
    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");
          die('Could not Connect MySql Server:' .mysql_error());
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. In html block we imported bootstrap cdn css link for design simply webpage. We defined form with method attribute value ‘post’ and class attribute values ‘text-center p-4’.
  4. The class ‘text-center’ used to align elements at center, ‘p-4’ used to set padding as 4px. Within form we defined two input fields, one is for collect user id and another one is for submit button.
  5. We designed webpage using bootstrap then we printed result on bootstrap card. When we using bootstrap we just need to add predefined class values it will change our webpage looks formally with proper alignments and responsive.
  6. In card-body we embedded php for displaying retrieved results and variable $singleRow had row of result in table.
  7. In php we included dbcon.php file because we executed database connection on this file our database name is ‘dbase’.
  8. The mysqli_connect() method will executes database connection for that we need to pass four parameters ‘host name, user name, password, database name’ properly then only we can successfully make connection otherwise it throws error.
  9. When we work with database this connection is must otherwise we can’t achieve any results. Using if() condition we checks submit is set or not by isset() method if they set then collected user entered value and stored to variable $uid.
  10. Using select query with condition to checks ‘$uid’ match with any id present in table ‘info’ if they not match with each other then it returns false.
  11. Otherwise matched row table data fetched and stored to variable ‘$singleRow’. Using this variable we can display username, password of matched id in table ‘info’.

Conclusion :-

In conclusion we are able to know how to get data from database using php.

First we need to start our xampp server then we load this program on browser we can see result of input field and card body with column names.

When user entered user id and clicks submit then if condition checks entered id is present in table or not.

If user entered id present then respective user name and password will fetched from database table displayed on card body successfully otherwise it returns null.

I hope this tutorial on how to get data from database in PHP using id helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪