Userless
Immersive Technology Specialist
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
100 XP
This tutorial will show you how to join 2 tables using left join in PHP/MySQLi. Joining tables is used to make 2 or more tables work as 1. In this tutorial, I will give you an idea how left join works and how to join to tables.
Creating our Database
First, we're going to create a database that contains the user data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "join_tutorial".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
Notice that we have created two tables in our database namely "user" and "user_details" both having a common column named "userid".
Inserting Data into our Tables
Next, we insert data into our tables. Make sure that the "userid" in one table have the same value in other table.
1. Click our database "join_tutorial".
2. Click SQL and paste the below code.
Creating our Connection
Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our page and our database. To create the file, open your HTML code editor and paste the code below after the tag.
Creating our Output Table
Lastly, we create a table that will output our query using LEFT JOIN.
To create the table, open your HTML code editor and paste the code below after the tag.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
Creating our Database
First, we're going to create a database that contains the user data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "join_tutorial".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
- CREATE
TABLE
`user`
(
- `userid`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
- `username`
VARCHAR
(
30
)
NOT
NULL
,
- `password`
VARCHAR
(
30
)
NOT
NULL
,
- PRIMARY
KEY
(
`userid`
)
- )
ENGINE=
InnoDB DEFAULT
CHARSET=
latin1;
- CREATE
TABLE
`user_details`
(
- `userid`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
- `firstname`
VARCHAR
(
30
)
NOT
NULL
,
- `lastname`
VARCHAR
(
30
)
NOT
NULL
,
- PRIMARY
KEY
(
`userid`
)
- )
ENGINE=
InnoDB DEFAULT
CHARSET=
latin1;

Notice that we have created two tables in our database namely "user" and "user_details" both having a common column named "userid".
Inserting Data into our Tables
Next, we insert data into our tables. Make sure that the "userid" in one table have the same value in other table.
1. Click our database "join_tutorial".
2. Click SQL and paste the below code.
- INSERT
INTO
`user`
(
`username`
,
`password`
)
VALUES
- (
'user1'
,
'user1'
)
,
- (
'user2'
,
'user2'
)
;
- INSERT
INTO
`user_details`
(
`firstname`
,
`lastname`
)
VALUES
- (
'neovic'
,
'devierte'
)
,
- (
'lee'
,
'ann'
)
;
Creating our Connection
Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our page and our database. To create the file, open your HTML code editor and paste the code below after the tag.
- <?php
- $conn
=
mysqli_connect
(
"localhost"
,
"root"
,
""
,
"join_tutorial"
)
;
- // Check connection
- if
(
mysqli_connect_errno
(
)
)
- {
- echo
"Failed to connect to MySQL: "
.
mysqli_connect_error
(
)
;
- }
- ?>
Creating our Output Table
Lastly, we create a table that will output our query using LEFT JOIN.
To create the table, open your HTML code editor and paste the code below after the tag.
- <!DOCTYPE html>
- <html
>
- <head
>
- <title
>
Joining Table using Left Join PHP,MySQLi</
title
>
- </
head
>
- <body
>
- <table
border
=
"1"
>
- <thead
>
- <th
>
Username</
th
>
- <th
>
Password</
th
>
- <th
>
Firstname</
th
>
- <th
>
Lastname</
th
>
- </
thead
>
- <tbody
>
- <?php
- include(
'conn.php'
)
;
- $query=
mysqli_query(
$conn,"select * from `user` left join user_details on user_details.userid=user.userid"
)
;
- while(
$row=
mysqli_fetch_array(
$query)
)
{
- ?>
- <tr
>
- <td
><?php echo $row[
'username'
]
; ?></
td
>
- <td
><?php echo $row[
'password'
]
; ?></
td
>
- <td
><?php echo $row[
'firstname'
]
; ?></
td
>
- <td
><?php echo $row[
'lastname'
]
; ?></
td
>
- </
tr
>
- <?php
- }
- ?>
- </
tbody
>
- </
table
>
- </
body
>
- </
html
>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view the hidden content.