Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Left Join in PHP Source Code

Last modified on January 7th, 2019 at 8:28 am

What is left join?

Left join shows all rows from the left table and shows only matching rows from the right table.


Table: Exams

Inner join shows results from both tables where there is any match between columns in both tables.


Table: Accounts

Now, we have two different tables. If we want to show the record of both tables in one single table then we can use the left join to join both two tables.

Query of the left join:

SELECT Exams. RegistrationNo, Exams.Marks, Accounts.Fee FROM Exams LEFT JOIN Accounts ON Exams.RegistrationNo =  Accounts.RegistrationNo


Table: Table generated after Left Join

Left Join Program Example in PHP, MySQL

Database for the code is shown below in the figure.

right join php mysql
Figure: Table brand, Table product, and Database t4
Sr.#PHP code with queryDescription


$connect = mysqli_connect(“localhost”, “root”, “”, “ali”);Database connection between php page and sql data base, “ali” is the database name and server is “localgost”





$sql = “SELECT * FROM brand LEFT JOIN prouct ON  prouct.pr_name = brand.br_id”;First of all we give the “$sql” variable and store the data form tow table using the “LEFT JOIN”.

It’s the “LEFT JOIN” query to get the right side Colum data in my browser page.




$result = mysqli_query($connect, $sql);

The “$result” variable get the database connection and SQL query.

“mysqli_query ()” is the function.  The “mysqli_query ()” perform the query against the database.




if (mysqli_num_rows($result) > 0)

It’s” if” condition used to compare the “$result” variable and any values, if the “$result” variable is grater then “0” then run the “mysqli_num_rows ()” function.

“mysqli_num_rows ()” return the number of values in result set.





while ($row = mysqli_fetch_array($result))

When the if condition is true then the “while” loop run,

The “while” loop include the “$row” variable and the”mysqli_fetch_array()” fuuction. The “mysqli_fetch_array()” function fetch data form database.

6<?php echo $row[“br_name”]; ?>“$row” variable Show data in web page form databases
7    <?php echo $row[“pr_name”]; ?>“$row” variable Show data in web page form databases


Prof. Fazal Rehman Shamil
Researcher, Publisher of International Journal Of Software Technology & Science ISSN: 2616-5325
Instructor, SEO Expert, Web Programmer and poet.
Feel free to contact.