Selecting MySQL Table Using Datatable in PHP

Submitted by: 
Language: 
Visitors have accessed this post 2880 times.

This tutorial will show you how to select mysql table with the use of data table. Datatable is an organize presentation on database table. It is often used in websites and php program because it has an in-built function like the search and pagination.

Creating our Database

First, we're going to create a database that contains the user data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "datatable".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` VARCHAR(30) NOT NULL,
  4. `lastname` VARCHAR(30) NOT NULL,
  5. `address` VARCHAR(150) NOT NULL,
  6. `birthdate` DATE NOT NULL,
  7. PRIMARY KEY (`userid`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

dt

Inserting Data into our Database

Next, we insert data into our database. These data will show as we select our table using datatable.
1. Click our database "datatable".
2. Click SQL and paste the below code.

  1. INSERT INTO `user` (`firstname`, `lastname`, `address`, `birthdate`) VALUES
  2. ('neovic', 'devierte', 'silay city', '1992-06-23'),
  3. ('lee', 'ann', 'silay city', '1990-01-01'),
  4. ('dee', 'dee', 'talisay city', '1996-08-09'),
  5. ('julyn', 'divinagracia', 'bacolod city', '1992-04-09');

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our page and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php
  2. $conn = mysqli_connect("localhost","root","","datatable");
  3.  
  4. // Check connection
  5. {
  6. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7. }
  8. ?>

Creating our Table

Lastly, we create our table using the datatable. We save this as "index.php". To create the table, open your HTML code editor and paste the code below after the tag.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <title>Datatable Example PHP MySQLi</title>
  4.  
  5. <link href="bootstrap.min.css" rel="stylesheet">
  6. <link href="dataTables.bootstrap.css" rel="stylesheet">
  7. <link href="dataTables.responsive.css" rel="stylesheet">
  8.  
  9. .mytable{
  10. margin-left:50px;
  11. margin-top:30px;
  12. width:1000px;
  13. }
  14. </head>
  15. <br>
  16. <div class="mytable">
  17. <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
  18. <tr>
  19. <th>UserId</th>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Address</th>
  23. <th>Birthday</th>
  24. </tr>
  25. </thead>
  26. <?php
  27. include('conn.php');
  28. $query=mysqli_query($conn,"select * from `user`");
  29. while($row=mysqli_fetch_array($query)){
  30. ?>
  31. <tr>
  32. <td><?php echo $row['userid']; ?></td>
  33. <td><?php echo $row['firstname']; ?></td>
  34. <td><?php echo $row['lastname']; ?></td>
  35. <td><?php echo $row['address']; ?></td>
  36. <td><?php echo $row['birthdate']; ?></td>
  37. </tr>
  38. <?php
  39. }
  40. ?>
  41. </tbody>
  42. </table>
  43. </div>
  44. <script src="jquery.min.js"></script>
  45. <script src="jquery.dataTables.min.js"></script>
  46. <script src="dataTables.bootstrap.min.js"></script>
  47. <script src="dataTables.responsive.js"></script>
  48. <script src="bootstrap.min.js"></script>
  49. $(document).ready(function() {
  50. $('#dataTables-example').DataTable({
  51. responsive: true
  52. });
  53. });
  54. </script>
  55. </body>
  56. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.