PHP - Simple Data Getter Using SQLite&Ajax

In this tutorial we will create a Simple Data Getter Using SQLite&Ajax. PHP is a server-side scripting language designed primarily for web development. SQLite 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. So Let's do the coding...

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 jquery that i used in this tutorial https://jquery.com/. Lastly, 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.  
  3.         $conn = new SQLite3('db/db_user.db');
  4.  
  5.         $query = "CREATE TABLE IF NOT EXISTS user (user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname VARCHAR, lastname VARCHAR, gender VARCHAR, age VARCHAR)";
  6.  
  7.         $conn->exec($query);
  8.        
  9. ?>

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 lnag="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="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">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 - Simple Data Getter Using SQLite&Ajax</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" id="btn_display"><span class="glyphicon glyphicon-book"></span> Display Data</button>
  18.                 <br /><br />
  19.                 <table class="table table-bordered">
  20.                         <thead class="alert-info">
  21.                                 <tr>
  22.                                         <th>Firstname</th>
  23.                                         <th>Lastname</th>
  24.                                         <th>Gender</th>
  25.                                         <th>Age</th>
  26.                                 </tr>
  27.                         </thead>
  28.                         <tbody style="background-color:#fff;" id="result"></tbody>
  29.                 </table>
  30.         </div>
  31. <script src="js/jquery-3.2.1.min.js"></script> 
  32. <script src="js/script.js"></script>
  33. </body>
  34. </html>

Creating the PHP Query

This code contains the php query of the application. This code will display the SQLite data as a HTML table. To do this just copy and write these block of codes as shown below inside the text editor and save it as data.php.
  1. <?php
  2.         require 'conn.php';
  3.  
  4.         $query = $conn->query('SELECT * FROM `user`');
  5.         while($fetch = $query->fetchArray()){
  6. ?>
  7. <tr>
  8.         <td><?php echo $fetch['firstname']?></td>
  9.         <td><?php echo $fetch['lastname']?></td>
  10.         <td><?php echo $fetch['gender']?></td>
  11.         <td><?php echo $fetch['age']?></td>
  12. </tr>
  13. <?php
  14.         }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will display the data from SQLite database by sending ajax request to the database server to fetch the within it, and display a readable table. To do this just copy and write these block of code inside the text editor and save it inside the js directory as script.js.
  1. $(document).ready(function(){
  2.         $('#btn_display').on('click', function(){
  3.                 $.ajax({
  4.                         url: 'data.php',
  5.                         type: 'POST',
  6.                         data: {res: 1},
  7.                         success: function(data){
  8.                                 $('#result').html(data);
  9.                         }
  10.                 });
  11.         })
  12. });
There you have it we successfully created a Simple Data Getter Using SQLite&Ajax. 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