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.
- Open localhost server folder XAMPP, etc and locate php.ini.
- Open php.ini and enable sqlite3 by removing the semicolon in the line.
- 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.
- <?php
- }
- $query="CREATE TABLE IF NOT EXISTS `member`(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT, phone_id INTEGER)";
- $query="CREATE TABLE IF NOT EXISTS `phone`(phone_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, phone_name TEXT)";
- $query1=$conn->query("SELECT COUNT(*) as count FROM `member`");
- $row1=$query1->fetchArray();
- $numRows1=$row1['count'];
- if($numRows1 == 0){
- }
- $query2=$conn->query("SELECT COUNT(*) as count FROM `phone`");
- $row2=$query2->fetchArray();
- $numRows2=$row2['count'];
- if($numRows2 == 0){
- }
- ?>
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.
- <?php require'conn.php'?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Combine Two Tables In SQLite3</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <center><h4>Owner</h4></center>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Member ID</th>
- <th>Name</th>
- </tr>
- </thead>
- <tbody>
- <?php
- while($fetch=$query->fetchArray()){
- echo"<tr><td>".$fetch['mem_id']."</td><td>".$fetch['name']."</td></tr>";
- }
- ?>
- </tbody>
- </table>
- <center><h4>Smart Phone</h4></center>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Phone ID</th>
- <th>Name</th>
- </tr>
- </thead>
- <tbody>
- <?php
- while($fetch=$query->fetchArray()){
- echo"<tr><td>".$fetch['phone_id']."</td><td>".$fetch['phone_name']."</td></tr>";
- }
- ?>
- </tbody>
- </table>
- </div>
- <div class="col-md-6">
- <form method="POST" action="">
- <center><button class="btn btn-primary" name="submit">Combine Table</button></center>
- </form>
- <br />
- <?php include'combine.php'?>
- </div>
- </div>
- </body>
- </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.
- <?php
- ?>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>ID</th>
- <th>Owner Name</th>
- <th>Phone</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $query=$conn->query("SELECT * FROM `member` LEFT JOIN `phone` ON member.phone_id = phone.phone_id") or die("Failed to fetch row!");
- while($fetch=$query->fetchArray()){
- echo"<tr><td>".$fetch['mem_id']."</td><td>".$fetch['name']."</td><td>".$fetch['phone_name']."</td></tr>";
- }
- ?>
- </tbody>
- </table>
- <?php
- }
- ?>
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
- 964 views