How to Merge Two Table Using PHP and MySQLi

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.

How to Merge Two Table Using PHP and MySQL

t4tutorials_database_connection.php

<?php
	$conn=mysqli_connect("localhost", "root", "", "t4tutorials_database_Merge");

	if(!$conn){
		die(mysqli_error());
	}
?>

index.php

<?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

<?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