Combining Two Tables of SQLite3 Database in PHP Tutorial

In this tutorial, we will create a Combine Two Tables In SQLite3 using PHP. This code will combine the two existing tables in the SQLite3 database when the user clicks 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 to use 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.

Then, 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 SQLite3 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.         if(!is_dir('db')){
  3.                 mkdir('db');
  4.         }
  5.         $conn=new SQLite3('db/db_member') or die("Unable to open database!");
  6.         $query="CREATE TABLE IF NOT EXISTS `member`(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT, phone_id INTEGER)";  
  7.         $conn->exec($query);
  8.         $query="CREATE TABLE IF NOT EXISTS `phone`(phone_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, phone_name TEXT)";     
  9.         $conn->exec($query);
  10.        
  11.         $query1=$conn->query("SELECT COUNT(*) as count FROM `member`");
  12.         $row1=$query1->fetchArray();
  13.         $numRows1=$row1['count'];
  14.        
  15.         if($numRows1 == 0){
  16.                 $conn->exec("INSERT INTO `member` (name, phone_id) VALUES('John Smith', 1)");
  17.                 $conn->exec("INSERT INTO `member` (name, phone_id) VALUES('Claire Temple', 2)");
  18.         }
  19.        
  20.        
  21.         $query2=$conn->query("SELECT COUNT(*) as count FROM `phone`");
  22.         $row2=$query2->fetchArray();
  23.         $numRows2=$row2['count'];
  24.        
  25.         if($numRows2 == 0){
  26.                 $conn->exec("INSERT INTO `phone` (phone_name) VALUES('Samsung Galaxy S11')");
  27.                 $conn->exec("INSERT INTO `phone` (phone_name) VALUES('Iphone XI')");
  28.         }
  29. ?>

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="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Combine Two Tables In SQLite3</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-6">
  19.                         <center><h4>Owner</h4></center>
  20.                         <table class="table table-bordered">
  21.                                 <thead class="alert-info">
  22.                                         <tr>
  23.                                                 <th>Member ID</th>
  24.                                                 <th>Name</th>
  25.                                         </tr>
  26.                                 </thead>
  27.                                 <tbody>
  28.                                         <?php
  29.                                                 $query=$conn->query("SELECT * FROM `member`") or die("Failed to fetch row!");
  30.                                                 while($fetch=$query->fetchArray()){
  31.                                                         echo"<tr><td>".$fetch['mem_id']."</td><td>".$fetch['name']."</td></tr>";
  32.                                                 }
  33.                                         ?>
  34.                                 </tbody>
  35.                         </table>
  36.                         <center><h4>Smart Phone</h4></center>
  37.                         <table class="table table-bordered">
  38.                                 <thead class="alert-info">
  39.                                         <tr>
  40.                                                 <th>Phone ID</th>
  41.                                                 <th>Name</th>
  42.                                         </tr>
  43.                                 </thead>
  44.                                 <tbody>
  45.                                         <?php
  46.                                                 $query=$conn->query("SELECT * FROM `phone`") or die("Failed to fetch row!");
  47.                                                 while($fetch=$query->fetchArray()){
  48.                                                         echo"<tr><td>".$fetch['phone_id']."</td><td>".$fetch['phone_name']."</td></tr>";
  49.                                                 }
  50.                                         ?>
  51.                                 </tbody>
  52.                         </table>
  53.                 </div>
  54.                 <div class="col-md-6">
  55.                         <form method="POST" action="">
  56.                                 <center><button class="btn btn-primary" name="submit">Combine Table</button></center>
  57.                         </form>
  58.                         <br />
  59.                         <?php include'combine.php'?>
  60.                 </div>
  61.         </div>
  62. </body>
  63. </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>Phone</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. ?>

DEMO

There you have it we successfully created a Combine Two Tables In SQLite3 using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Add new comment