How to Merge Two Table Using PHP and MySQLi?
Let’s see how to Merge the Two Tables using PHP and MySQLi. The program will merge the two tables that are already created in PHPMyAdmin. When the user clicks the merge button, the merging of tables occurs. The code uses a MySQLi SELECT() function and adds a LEFT JOIN parameter to merge the two tables into a third view table that has the same keys in both.
t4tutorials_database_connection.php
1 2 3 4 5 6 7 |
<?php $conn=mysqli_connect("localhost", "root", "", "t4tutorials_database_Merge"); if(!$conn){ die(mysqli_error()); } ?> |
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?php require't4tutorials_database_connection.php'?> <!DOCTYPE html> <html> <head> <style> table { background-color:#FFFF99; border:thick; } th {background-color:green; border:thick; } h4 { float:left; color:#0000FF; } button { float:left; background-color:#99CC99; } </style> </head> <body> <div> <h3>How to Merge Two Table Using PHP and MySQLi</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>ID</th> <th>Name</th> </tr> </thead>
<tbody> <?php $query=mysqli_query($conn, "SELECT * FROM `owner`") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $fetch['owner_id']?></td> <td><?php echo $fetch['owner_name']?></td> </tr> <?php } ?> </tbody> </table> <center><h4>Car</h4></center> <br><br><table class="table table-bordered"> <thead class="alert-info"> <tr> <th>ID</th> <th>Name</th> </tr>
</thead> <tbody> <?php $query=mysqli_query($conn, "SELECT * FROM `car`") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $fetch['car_id']?></td> <td><?php echo $fetch['car_name']?></td> </tr> <?php } ?> </tbody> </table> </div> <div class="col-md-6"> <br><form method="POST" action=""> <center><button name="submit">Click here to Merge two Tables</button></center> </form> <br /> <?php include'merge_tables.php'?> </div> </div> </body> </html> |
merge_tables.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php if(ISSET($_POST['submit'])){ ?> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th>ID</th> <th>Owner Name</th> <th>Name</th> </tr> </thead> <tbody> <?php $query=mysqli_query($conn, "SELECT * FROM `owner` LEFT JOIN `car` ON owner.car_id = car.car_id") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $fetch['owner_id']?></td> <td><?php echo $fetch['owner_name']?></td> <td><?php echo $fetch['car_name']?></td> </tr> <?php } ?> </tbody> </table> <?php } ?> |
Download Code
How to Merge Two Table Using PHP and MySQLi
Database t4tutorials_database_merge
Table structure for table car
Column | Type | Null | Default |
car_id | int(11) | No | |
car_name | varchar(100) | No |
Dumping data for table car
1 | Suzuki |
2 | Toyota |
Table structure for table owner
Column | Type | Null | Default |
owner_id | int(11) | No | |
owner_name | varchar(100) | No | |
car_id | int(11) | No |
Dumping data for table owner
1 | fazal | 1 |
2 | sharjeel | 2 |
3 | sameed | 1 |
4 | shahzeb | 1 |