What are the advantages of using JOIN statements in MySQL queries within PHP scripts?

When working with relational databases like MySQL in PHP scripts, JOIN statements are crucial for retrieving data from multiple tables based on a related column. Using JOIN statements helps reduce the number of queries needed to fetch related data, improving performance and reducing the complexity of the code.

<?php
// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Query using JOIN statement to fetch data from multiple tables
$query = "SELECT users.username, orders.order_date 
          FROM users 
          JOIN orders ON users.id = orders.user_id";

$result = mysqli_query($connection, $query);

// Loop through the results
while($row = mysqli_fetch_assoc($result)) {
    echo "Username: " . $row['username'] . " | Order Date: " . $row['order_date'] . "<br>";
}

// Close the connection
mysqli_close($connection);
?>