What are some common pitfalls when using multiple SELECT queries in PHP and how can they be avoided?

One common pitfall when using multiple SELECT queries in PHP is not properly closing the connection to the database after each query, which can lead to resource leaks and potential performance issues. To avoid this, always make sure to close the database connection after executing each query.

// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');

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

// First SELECT query
$query1 = "SELECT * FROM table1";
$result1 = $connection->query($query1);

// Process result of first query

// Close connection after first query
$connection->close();

// Reconnect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');

// Second SELECT query
$query2 = "SELECT * FROM table2";
$result2 = $connection->query($query2);

// Process result of second query

// Close connection after second query
$connection->close();