How to Combine Two Tables In SQLite3 using PHP

July 13, 2020
PHP
How to Combine Two Tables In SQLite3 using PHP

Tutorial: How to Combine Two Tables In SQLite3 using PHP with Source Code

In this tutorial, we will create on How to 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 system uses an SQLite SELECT() function and by adding a LEFT JOIN parameter to combine the two existing tables into one that has the same key in both rows. This is a user-friendly kind of program feel free to the user in your application.

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 runs PHP scripts. Here’s the link for the 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.

How to Combine Two Tables In SQLite3 using PHP

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
    $conn=new SQLite3('db/db_member') or die("Unable to open database!");
    $query="CREATE TABLE IF NOT EXISTS `member`(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT, phone_id INTEGER)";	
    $conn->exec($query);
    $query="CREATE TABLE IF NOT EXISTS `phone`(phone_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, phone_name TEXT)";	
    $conn->exec($query);
 
    $query1=$conn->query("SELECT COUNT(*) as count FROM `member`");
    $row1=$query1->fetchArray();
    $numRows1=$row1['count'];
 
    if($numRows1 == 0){
        $conn->exec("INSERT INTO `member` (name, phone_id) VALUES('John Smith', 1)");
        $conn->exec("INSERT INTO `member` (name, phone_id) VALUES('Claire Temple', 2)");
    }
 
 
    $query2=$conn->query("SELECT COUNT(*) as count FROM `phone`");
    $row2=$query2->fetchArray();
    $numRows2=$row2['count'];
 
    if($numRows2 == 0){
        $conn->exec("INSERT INTO `phone` (phone_name) VALUES('Samsung Galaxy S11')");
        $conn->exec("INSERT INTO `phone` (phone_name) VALUES('Iphone XI')");
    }
?>

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
                        $query=$conn->query("SELECT * FROM `member`") or die("Failed to fetch row!");
                        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
                        $query=$conn->query("SELECT * FROM `phone`") or die("Failed to fetch row!");
                        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
    if(ISSET($_POST['submit'])){
?>
<table class="table table-bordered">
    <thead class="alert-info">
        <tr>
            <th>ID</th>
            <th>Owner Name</th>
            <th>Car</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
    }
?>

There you have it we successfully created on How to 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!!!

Leave a Reply

Your email address will not be published. Required fields are marked *