PHP - Display MySQLi Result In DataTable

Submitted by: 
Visitors have accessed this post 650 times.

In this tutorial we will create a Display MySQLi Result In DataTable using PHP. This code will automatically change table properties into a interactive advance table management. The code use DataTable to contain the data 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 use DataTable as a JavaScript framework that handle and manage the data within the table. It has a wealth of options which can be used to configure that obtain and process the data in a complex table.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Creating Database

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

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 = mysqli_connect("localhost", "root", "", "db_data_table");
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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="<a href="">Sourcecodester</a>
  12. " rel="nofollow">">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Display MySQLi Result In DataTable</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-4">
  20. <form method="POST" action="save.php">
  21. <div class="form-group">
  22. <label>Firstname</label>
  23. <input type="text" class="form-control" name="firstname" required="required"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Lastname</label>
  27. <input type="text" class="form-control" name="lastname" required="required"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Address</label>
  31. <input type="text" class="form-control" name="address" required="required"/>
  32. </div>
  33. <center><button name="save" class="btn btn-primary">Save</button></center>
  34. </form>
  35. </div>
  36. <div class="col-md-8">
  37. <table id="table" class="table table-bordered">
  38. <thead class="alert-info">
  39. <tr>
  40. <th>Firstname</th>
  41. <th>Lastname</th>
  42. <th>Address</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <?php
  47. require 'conn.php';
  49. $query = mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  50. while($fetch = mysqli_fetch_array($query)){
  51. ?>
  52. <tr>
  53. <td><?php echo $fetch['firstname']?></td>
  54. <td><?php echo $fetch['lastname']?></td>
  55. <td><?php echo $fetch['address']?></td>
  56. </tr>
  57. <?php
  58. }
  59. ?>
  60. </tbody>
  61. </table>
  62. </div>
  63. </div>
  66. <script src="js/jquery-3.2.1.min.js"></script>
  67. <script src="js/bootstrap.js"></script>
  68. <script src = "js/jquery.dataTables.js"></script>
  69. </body>
  70. </html>

Creating PHP Query

This code contains the php query of the application. This code save the user data inputs to the MySQLi 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';
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $address = $_POST['address'];
  9. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  11. header("location: index.php");
  12. }
  13. ?>

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 Display MySQLi Result In DataTable using PHP. 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.