How can using multiple queries in a PHP script impact its performance and functionality?

Using multiple queries in a PHP script can impact its performance and functionality by increasing the load on the database server and potentially slowing down the script execution. To improve performance, you can combine multiple queries into a single query using JOINs or subqueries to reduce the number of round trips to the database.

// Example of combining multiple queries into a single query using JOIN
$query = "SELECT users.id, users.name, orders.order_date 
          FROM users
          JOIN orders ON users.id = orders.user_id
          WHERE users.status = 'active'";

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

// Process the result set
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Process each row
    }
} else {
    echo "No records found.";
}

mysqli_free_result($result);
mysqli_close($connection);