Blog » PHP » joining tables using php mysqli

joining tables using php mysqli

This article will show you how to join two tables using self join in PHP/MySQLi. if we have lot of tables row in to display on product page we have to joining tables is used to make two or more tables works as a one table. therefor we have to joining two or more tables and usefulness this code to joining tables. I have used PHP Object oriented programming to create this one.you can get idea to how joining more tables.

Here is the completed source code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>join table</title>
<style>
#table-s {font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size:12px;border-collapse: collapse;}
table{
width:280px; 
border:solid 1px #eee;
}
th, col{
border:solid 1px #eee;
text-align: left;padding:5px;
}
td
{
border:solid 1px #eee; 
text-align: left;
padding:10px;
}
td img{
width:80px;height:70px;}
</style>
</head>
<body>
<table width="" height="" id="table-s">
<tr><th scope="col">Products</th><th scope="col">Brand</th></tr>
<?php 
$hostname = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "join";
$con = new mysqli($hostname, $dbuser, $dbpass, $dbname);
if($con->connect_error){
die("Error connect to db");
}
if ($stmt = $con->prepare("SELECT p.*, b.brand_name, title, id FROM products AS p 
JOIN brand AS b ON b.brand_id = p.brand_id
order by id desc")){
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['brand_name'];?></td>
</tr><?php
}
}
?>
</table>
</body>
</html>
ARTICLE POSTED BY : Admin

Hi, I'am k.g uththara. I'm a professional web developer. I write blog posts in my free time. if you want to contact me, send a message on contact page, thanks.