Pretty Print SQLite Table Using PHP Source Code

In this tutorial we will create a Pretty Print SQLite Table using PHP. This code will petty print your SQLite data table when the user click the button. The code use a PHP fetchArray() to fetch the data in a while loop to extract the SQLite data in order to display as a readable array using print_r(). This is a user-friendly kind of program feel free user it in your program. We will be using SQLite that provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP.

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/. Installing SQLite Browser We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP.
  1. Open localhost server folder XAMPP, etc and locate php.ini.
  2. Open php.ini and enable sqlite3 by removing the semicolon in the line. tut1
  3. Save changes and Restart Server.

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 SQLite3('db/db_member') or die("Unable to open database!");
  3. $query="CREATE TABLE IF NOT EXISTS `member`(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, address TEXT)";
  4.  
  5. $conn->exec($query);
  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 you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="https://sourcecodester.com" 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">Pretty Print SQLite Table Using PHP Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;" />
  17.  
  18. <div class="col-md-6">
  19. <form method="POST" action="">
  20. <button class="btn btn-primary" name="print">Pretty Print</button>
  21. </form>
  22. <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>
  32. <?php
  33. require 'conn.php';
  34. $query=$conn->query("SELECT * FROM `member` ORDER BY `lastname` ASC") or die("Failed to fetch row!");
  35. while($fetch=$query->fetchArray()){
  36. ?>
  37. <tr>
  38. <td><?php echo $fetch['firstname']?></td>
  39. <td><?php echo $fetch['lastname']?></td>
  40. <td><?php echo $fetch['address']?></td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. <?php
  49. include'print.php';
  50. ?>
  51. </div>
  52. </body>
  53. </html>

Creating the Main Function

This code contains the main function of the application. This code will pretty print a SQLite table when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor, then save it as print.php.
  1. <div class="col-md-6">
  2. <?php
  3. if(ISSET($_POST['print'])){
  4. $query=$conn->query("SELECT * FROM `member` ORDER BY `lastname` ASC") or die("Failed to fetch row!");
  5. echo"<pre>";
  6. while($fetch=$query->fetchArray()){
  7. print_r($fetch);
  8. }
  9. echo"</pre>";
  10. }
  11. ?>
  12. </div>
There you have it we successfully created a Pretty Print SQLite Table 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