PHP - Simple Data Getter Using SQLite&Ajax

Submitted by: 
Visitors have accessed this post 290 times.

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

And this is the link for the jquery that i used in this tutorial

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

Installing SQLite Browser
We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite

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.
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
  3. $conn = new SQLite3('db/db_user.db');
  7. $conn->exec($query);
  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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Data Getter Using SQLite&Ajax</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button type="button" class="btn btn-primary" id="btn_display"><span class="glyphicon glyphicon-book"></span> Display Data</button>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Gender</th>
  26. <th>Age</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;" id="result"></tbody>
  30. </table>
  31. </div>
  32. <script src="js/jquery-3.2.1.min.js"></script>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </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';
  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

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.