Joining Table using Left Join PHP/MySQL

Submitted by: 
Visitors have accessed this post 1659 times.

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.

  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(30) NOT NULL,
  4. `password` VARCHAR(30) NOT NULL,
  5. PRIMARY KEY (`userid`)
  8. CREATE TABLE `user_details` (
  9. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  10. `firstname` VARCHAR(30) NOT NULL,
  11. `lastname` VARCHAR(30) NOT NULL,
  12. PRIMARY KEY (`userid`)


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.

  1. INSERT INTO `user` (`username`, `password`) VALUES
  2. ('user1', 'user1'),
  3. ('user2', 'user2');
  5. INSERT INTO `user_details` (`firstname`, `lastname`) VALUES
  6. ('neovic', 'devierte'),
  7. ('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.

  1. <?php
  2. $conn = mysqli_connect("localhost","root","","join_tutorial");
  4. // Check connection
  5. {
  6. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7. }
  8. ?>

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.

  1. <!DOCTYPE html>
  2. <title>Joining Table using Left Join PHP,MySQLi</title>
  3. </head>
  4. <table border="1">
  5. <th>Username</th>
  6. <th>Password</th>
  7. <th>Firstname</th>
  8. <th>Lastname</th>
  9. </thead>
  10. <?php
  11. include('conn.php');
  12. $query=mysqli_query($conn,"select * from `user` left join user_details on user_details.userid=user.userid");
  13. while($row=mysqli_fetch_array($query)){
  14. ?>
  15. <tr>
  16. <td><?php echo $row['username']; ?></td>
  17. <td><?php echo $row['password']; ?></td>
  18. <td><?php echo $row['firstname']; ?></td>
  19. <td><?php echo $row['lastname']; ?></td>
  20. </tr>
  21. <?php
  22. }
  23. ?>
  25. </tbody>
  26. </table>
  27. </body>
  28. </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 for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


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.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.