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 can get idea to how joining more tables.

Here is the completed source code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>join table</title>
#table-s {font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size:12px;border-collapse: collapse;}
border:solid 1px #eee;
th, col{
border:solid 1px #eee;
text-align: left;padding:5px;
border:solid 1px #eee; 
text-align: left;
td img{
<table width="" height="" id="table-s">
<tr><th scope="col">Products</th><th scope="col">Brand</th></tr>
$hostname = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "join";
$con = new mysqli($hostname, $dbuser, $dbpass, $dbname);
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")){
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
<td><?php echo $row['title'];?></td>
<td><?php echo $row['brand_name'];?></td>

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.