What are the potential pitfalls of using multiple queries in a loop in PHP?

Using multiple queries in a loop in PHP can lead to performance issues as each query requires a connection to the database and consumes resources. To solve this, you can combine the queries into a single query using JOINs or subqueries to reduce the number of database connections.

// Example of combining multiple queries into a single query using JOIN
$query = "SELECT users.*, orders.* FROM users 
          LEFT JOIN orders ON users.id = orders.user_id";

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

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        // Process the combined data
    }
}