Right Join in PHP Source Code

Table of Contents

What is right join?

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

RegistrationNoMarks
T4Tutorials177
T4Tutorials232
T4Tutorials455

Table: Exams

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

RegistrationNoFee
T4Tutorials11000
T4Tutorials22000

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 right join to join both two tables.

Query.

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

In this query Accounts is the right table.

 

RegistrationNoMarksFee
T4Tutorials1771000
T4Tutorials2322000

Table: Generated after Right Join.

Right Join Program Example in PHP.

Database for the code is shown in the figure below.

right join php mysql

Figure: Table brand and Table product, Database is t4.

<?php 
$connection = mysqli_connect("localhost", "root", "", "t4");
$sql = "SELECT * FROM prouct RIGHT JOIN brand ON   brand.br_id =prouct.pr_id";
$result = mysqli_query($connection, $sql);
?>
<!DOCTYPE html>
<html>
<head>
	<title>How to join table using php wiht sql</title>
    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<br/>
<div class="container">
	<h3 align="">Fetch Data from two table join</h3>
	<div class="table-resposive">
		<table class="table table-resposive">
			<tr>
				<th>Brand Name</th>
				<th>Product Name</th>
			</tr>
			<?php 
				if (mysqli_num_rows($result) > 0) {
					while ($row = mysqli_fetch_array($result)) {
					?>	
					<tr>
						<td><?php echo $row["br_name"]; ?></td>
						<td><?php echo $row["pr_name"]; ?></td>
					</tr>
					<?php 
					}
				}
			?>
		</table>
	</div>
</div>
</body>
</html>
Sr.#PHP code with queryDescription
 

1

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

 

 

 

2

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

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

 

3

 

$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.

 

4

 

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.

 

 

5

 

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
Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.

Leave a Reply

Your email address will not be published. Required fields are marked *