PHP - Combine Two Tables In SQLite3

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

In this tutorial we will create a Combine Two Tables In SQLite3 using PHP. This code will combine the two existing table in the SQLite3 database when user click the merge button. The code use a SQLite SELECT() function and by adding a LEFT JOIN parameter in order to combine the two existing table into one that has a same key in both rows. 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 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. $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, name TEXT, phone_id INTEGER)";
  4. $conn->exec($query);
  5. $query="CREATE TABLE IF NOT EXISTS `phone`(phone_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, phone_name TEXT)";
  6. $conn->exec($query);
  7.  
  8. $query1=$conn->query("SELECT COUNT(*) as count FROM `member`");
  9. $row1=$query1->fetchArray();
  10. $numRows1=$row1['count'];
  11.  
  12. if($numRows1 == 0){
  13. $conn->exec("INSERT INTO `member` (name, phone_id) VALUES('John Smith', 1)");
  14. $conn->exec("INSERT INTO `member` (name, phone_id) VALUES('Claire Temple', 2)");
  15. }
  16.  
  17.  
  18. $query2=$conn->query("SELECT COUNT(*) as count FROM `phone`");
  19. $row2=$query2->fetchArray();
  20. $numRows2=$row2['count'];
  21.  
  22. if($numRows2 == 0){
  23. $conn->exec("INSERT INTO `phone` (phone_name) VALUES('Samsung Galaxy S11')");
  24. $conn->exec("INSERT INTO `phone` (phone_name) VALUES('Iphone XI')");
  25. }
  26. ?>

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. <?php require'conn.php'?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">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 - Combine Two Tables In SQLite3</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-6">
  20. <center><h4>Owner</h4></center>
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Member ID</th>
  25. <th>Name</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <?php
  30. $query=$conn->query("SELECT * FROM `member`") or die("Failed to fetch row!");
  31. while($fetch=$query->fetchArray()){
  32. echo"<tr><td>".$fetch['mem_id']."</td><td>".$fetch['name']."</td></tr>";
  33. }
  34. ?>
  35. </tbody>
  36. </table>
  37. <center><h4>Smart Phone</h4></center>
  38. <table class="table table-bordered">
  39. <thead class="alert-info">
  40. <tr>
  41. <th>Phone ID</th>
  42. <th>Name</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <?php
  47. $query=$conn->query("SELECT * FROM `phone`") or die("Failed to fetch row!");
  48. while($fetch=$query->fetchArray()){
  49. echo"<tr><td>".$fetch['phone_id']."</td><td>".$fetch['phone_name']."</td></tr>";
  50. }
  51. ?>
  52. </tbody>
  53. </table>
  54. </div>
  55. <div class="col-md-6">
  56. <form method="POST" action="">
  57. <center><button class="btn btn-primary" name="submit">Combine Table</button></center>
  58. </form>
  59. <br />
  60. <?php include'combine.php'?>
  61. </div>
  62. </div>
  63. </body>
  64. </html>

Creating the Main Function

This code contains the main function of the application. This code will combine the two tables in the SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as combine.php.

  1. <?php
  2. if(ISSET($_POST['submit'])){
  3. ?>
  4. <table class="table table-bordered">
  5. <thead class="alert-info">
  6. <tr>
  7. <th>ID</th>
  8. <th>Owner Name</th>
  9. <th>Car</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <?php
  14. $query=$conn->query("SELECT * FROM `member` LEFT JOIN `phone` ON member.phone_id = phone.phone_id") or die("Failed to fetch row!");
  15. while($fetch=$query->fetchArray()){
  16. echo"<tr><td>".$fetch['mem_id']."</td><td>".$fetch['name']."</td><td>".$fetch['phone_name']."</td></tr>";
  17. }
  18. ?>
  19. </tbody>
  20. </table>
  21. <?php
  22. }
  23. ?>

There you have it we successfully created a Combine Two Tables In SQLite3 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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.