Why is it recommended to establish a database connection only once per script execution in PHP?

Establishing a database connection multiple times in a script can lead to inefficiency and unnecessary overhead. It is recommended to establish a database connection only once per script execution to improve performance and reduce resource consumption.

// Establishing a database connection only once per script execution
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

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