PHP - Simple Join Table Using MySQLi

In this tutorial we will create a Simple Join Table using MySQLi. This code will join the two existing table in the MySQLi database when user click the join button. The code itself self use a MySQLi SELECT() function that add a LEFT JOIN parameter to join the the two table that has a same key existing to each composition. This is a user-friendly kind of program feel free user it in your program. We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

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 bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_join, after that click Import then locate the database file inside the folder of the application then click ok. tut1

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=mysqli_connect("localhost", "root", "", "db_join");
  3.  
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

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 your 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 - Simple Join Table Using MySQLi</h3>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-6">
  19.                         <center><h4>STUDENT</h4></center>
  20.                         <table class="table table-bordered">
  21.                                 <thead>
  22.                                         <tr>
  23.                                                 <th>Firstname</th>
  24.                                                 <th>Lastname</th>
  25.                                         </tr>
  26.                                 </thead>
  27.                                 <tbody>
  28.                                         <?php
  29.                                                 $query=mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  30.                                                 while($fetch=mysqli_fetch_array($query)){
  31.                                         ?>
  32.                                         <tr>
  33.                                                 <td><?php echo $fetch['firstname']?></td>
  34.                                                 <td><?php echo $fetch['lastname']?></td>
  35.                                         </tr>
  36.                                         <?php
  37.                                                 }
  38.                                         ?>
  39.                                 </tbody>
  40.                         </table>
  41.                 </div>
  42.                 <div class="col-md-6">
  43.                         <center><h4>TEACHER</h4></center>
  44.                         <table class="table table-bordered">
  45.                                 <thead>
  46.                                         <tr>
  47.                                                 <th>Firstname</th>
  48.                                                 <th>Lastname</th>
  49.                                         </tr>
  50.                                 </thead>
  51.                                 <tbody>
  52.                                         <?php
  53.                                                 $query=mysqli_query($conn, "SELECT * FROM `teacher`") or die(mysqli_error());
  54.                                                 while($fetch=mysqli_fetch_array($query)){
  55.                                         ?>
  56.                                         <tr>
  57.                                                 <td><?php echo $fetch['teacher_firstname']?></td>
  58.                                                 <td><?php echo $fetch['teacher_lastname']?></td>
  59.                                         </tr>
  60.                                         <?php
  61.                                                 }
  62.                                         ?>
  63.                                 </tbody>
  64.                         </table>
  65.                 </div>
  66.                 <br style="clear:both;"/>
  67.                 <div class="col-md-2"></div>
  68.                 <div class="col-md-8">
  69.                         <form method="POST" action="">
  70.                                 <center><button class="btn btn-primary" name="join">Join Table</button></center>
  71.                         </form>
  72.                         <br />
  73.                         <?php include'join.php'?>
  74.                 </div>
  75.         </div>
  76. </body>
  77. </html>

Creating the Main Function

This code contains the main function of the application. This code will merge the two table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as join.php.
  1. <?php
  2.         if(ISSET($_POST['join'])){
  3. ?>
  4. <table class="table table-bordered">
  5.         <thead>
  6.                 <tr>
  7.                         <th>Firstname</th>
  8.                         <th>Lastname</th>
  9.                         <th>Teacher</th>
  10.                 </tr>
  11.         </thead>
  12.         <tbody>
  13.                 <?php
  14.                         $query=mysqli_query($conn, "SELECT * FROM `student` LEFT JOIN `teacher` ON student.teach_id = teacher.teach_id") or die(mysqli_error());
  15.                         while($fetch=mysqli_fetch_array($query)){
  16.                 ?>
  17.                 <tr>
  18.                         <td><?php echo $fetch['firstname']?></td>
  19.                         <td><?php echo $fetch['lastname']?></td>
  20.                         <td><?php echo $fetch['teacher_firstname']." ".$fetch['teacher_lastname']?></td>
  21.                 </tr>
  22.                 <?php
  23.                         }
  24.                 ?>
  25.         </tbody>
  26. </table>
  27. <?php
  28.         }
  29. ?>
There you have it we successfully created Simple Join Table using MySQLi. 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