What potential pitfalls can arise when using foreach loops to execute multiple MySQL queries in PHP?

When using foreach loops to execute multiple MySQL queries in PHP, a potential pitfall is that it can lead to multiple database connections being opened and closed for each iteration, which can impact performance. To solve this issue, it's recommended to open a single database connection outside of the loop and reuse it for all queries within the loop.

// Establish a database connection
$connection = new mysqli($host, $username, $password, $database);

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

// Loop through an array of queries
foreach ($queries as $query) {
    // Execute the query
    $result = $connection->query($query);
    
    // Process the result
    // ...
}

// Close the database connection
$connection->close();