PHP - Display Data From Database Table Using PDO

In this tutorial we will create a Display Data From Database Table using PDO. This code will display a PDO data from database table when user click the button. The code use a PDO SELECT query to read all the available data in the database then display it as a readable content using fetch(). This is a user friendly kind of program, feel free to modify it. We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_pdo_table, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_table', 'root', '');
  3.         if(!$conn){
  4.                 die("Fatal Error: Connection Failed!");
  5.         }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="cotaniner-fluid">
  10.                         <a class="navbar-brand">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Display Data From Database Table Using PDO</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-2"></div>
  18.                 <div class="col-md-8">
  19.                         <form method="POST" action="">
  20.                                 <button class="btn btn-primary" name="display">Display PDO Table</button>
  21.                         </form>
  22.                         <br /><br />
  23.                         <table class="table table-bordered">
  24.                                 <thead class="alert-info">
  25.                                         <tr>
  26.                                                 <th>Firstname</th>
  27.                                                 <th>Lastname</th>
  28.                                                 <th>Address</th>
  29.                                         </tr>
  30.                                 </thead>
  31.                                 <tbody style="background-color:#fff;">
  32.                                         <?php include'pdo_table.php'?>
  33.                                 </tbody>
  34.                         </table>
  35.                 </div> 
  36.         </div>
  37. </body>
  38. </html>

Creating the Main Function

This code contains the main function of the application. This code will display data from the database when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as pdo_table.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['display'])){
  5.                 $sql = "SELECT * FROM `user`";
  6.                 $query = $conn->prepare($sql);
  7.                 $query->execute();
  8.                
  9.                 while($fetch = $query->fetch()){
  10. ?>
  11.  
  12. <tr>
  13.         <td><?php echo $fetch['firstname']?></td>
  14.         <td><?php echo $fetch['lastname']?></td>
  15.         <td><?php echo $fetch['address']?></td>
  16. </tr>
  17.  
  18. <?php
  19.                 }
  20.         }
  21. ?>
There you have it we successfully created a Display Data From Database Table using PDO. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

Dear Sir, Good Morning. Many many thanks to you for giving most valuable tips for PDO. thanks/freards Asok Goswami.

Add new comment