PHP - Display PDO Result In DataTable

In this tutorial we will create a Display PDO Result In DataTable using PDO. This code can will display PDO result in an organized table layout. The code use DataTable to contain the PDO data result with a interactive table design by just binding the id of the table.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_datatable, 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_datatable', 'root', '');
  3.         if(!$conn){
  4.                 die("Error: Failed to coonect to database!");
  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.                 <link rel = "stylesheet" type = "text/css" href = "css/jquery.dataTables.css" />
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Display PDO Result In DataTable</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-4">
  19.                         <form method="POST" action="save.php">
  20.                                 <div class="form-group">
  21.                                         <label>Firstname</label>
  22.                                         <input type="text" class="form-control" name="firstname" required="required"/>
  23.                                 </div>
  24.                                 <div class="form-group">
  25.                                         <label>Lastname</label>
  26.                                         <input type="text" class="form-control" name="lastname" required="required"/>
  27.                                 </div>
  28.                                 <div class="form-group">
  29.                                         <label>Address</label>
  30.                                         <input type="text" class="form-control" name="address" required="required"/>
  31.                                 </div>
  32.                                 <center><button name="save" class="btn btn-primary">Save</button></center>
  33.                         </form>
  34.                 </div> 
  35.                 <div class="col-md-8">
  36.                         <table id="table" class="table table-bordered">
  37.                                 <thead class="alert-info">
  38.                                         <tr>
  39.                                                 <th>Firstname</th>
  40.                                                 <th>Lastname</th>
  41.                                                 <th>Address</th>
  42.                                         </tr>
  43.                                 </thead>
  44.                                 <tbody>
  45.                                         <?php
  46.                                                 require'conn.php';
  47.                                                 $query = $conn->prepare("SELECT * FROM `member`");
  48.                                                 $query->execute();
  49.                                                 while($row = $query->fetch()){
  50.                                                         echo "<tr>
  51.                                                                 <td>".$row['firstname']."</td>
  52.                                                                 <td>".$row['lastname']."</td>
  53.                                                                 <td>".$row['address']."</td>
  54.                                                         </tr>";
  55.                                                 }
  56.                                         ?>
  57.                                 </tbody>
  58.                         </table>
  59.                 </div>
  60.         </div>
  61.        
  62.  
  63. <script src="js/jquery-3.2.1.min.js"></script>
  64. <script src="js/bootstrap.js"></script>
  65. <script src = "js/jquery.dataTables.js"></script>
  66. </body>
  67. </html>

Creating PDO Query

This code contains the PDO query of the application. This code save the user data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2.         require_once 'conn.php';
  3.  
  4.         if(ISSET($_POST['save'])){
  5.  
  6.                 $firstname = $_POST['firstname'];
  7.                 $lastname = $_POST['lastname'];
  8.                 $address = $_POST['address'];
  9.  
  10.  
  11.                 try{
  12.                         $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13.                         $sql = "INSERT INTO `member`(firstname, lastname, address)  VALUES ('$firstname', '$lastname', '$address')";
  14.                         $conn->exec($sql);
  15.                 }catch(PDOException $e){
  16.                         echo $e->getMessage();
  17.                 }
  18.  
  19.                 $conn = null;
  20.  
  21.                 header("location: index.php");
  22.  
  23.         }
  24. ?>

Creating the Main Function

This code contains the main function of the application. This code will change the table properties when applied as a javascript source.
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.         $('#table').DataTable({
  4.                 "order": [[2, "asc"]]
  5.         });
  6. });
  7. </script>
There you have it we successfully created a Display PDO Result In DataTable 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!

Add new comment