What is the significance of using JOIN in PHP when dealing with multiple tables in a database?

When dealing with multiple tables in a database, using JOIN in PHP allows you to combine data from different tables based on a related column between them. This is essential for querying and retrieving information that is spread across multiple tables in a relational database. By using JOIN, you can efficiently retrieve and manipulate data from multiple tables in a single query.

<?php

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL query using JOIN to retrieve data from multiple tables
$sql = "SELECT users.username, orders.order_id, orders.total_price FROM users
        JOIN orders ON users.user_id = orders.user_id";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Username: " . $row["username"]. " - Order ID: " . $row["order_id"]. " - Total Price: " . $row["total_price"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

?>