What is the purpose of using JOIN in PHP when working with multiple tables in a MySQL database?

When working with multiple tables in a MySQL database, the JOIN clause in PHP is used to combine rows from two or more tables based on a related column between them. This allows you to retrieve data from multiple tables in a single query, avoiding the need to make multiple queries and then manually combine the results.

$query = "SELECT * FROM table1
          JOIN table2 ON table1.column_name = table2.column_name";
$result = mysqli_query($connection, $query);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process the data here
    }
} else {
    echo "No results found";
}