inner join program code in PHP, MySQL

What is inner join?

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

RegistrationNo Marks
T4Tutorials1 77
T4Tutorials2 32
T4Tutorials3 88

Table: Exams

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

RegistrationNo Fee
T4Tutorials1 1000
T4Tutorials2 2000
T4Tutorials3 3000

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

Query of an inner join.

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

RegistrationNo Marks Fee
T4Tutorials1 77 1000
T4Tutorials2 32 2000
T4Tutorials3 88 3000

Table: Table generated after INNER Join

Inner Join Program Example in PHP, MYSQL

Database for the inner join program is shown below.

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

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 INNER JOIN prouct ON  prouct.br_id = brand.br_id”; First of all we give the “$sql” variable and store the data form tow table using the “INNER JOIN”.

It’s the “INNER  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