What are the potential issues with having multiple database connections in a single PHP script?

Having multiple database connections in a single PHP script can lead to confusion, inefficiency, and potential conflicts between the connections. To solve this issue, you can use a single database connection throughout the script by creating a single instance of the database connection and reusing it whenever needed.

// Create a single instance of the database connection
$connection = new mysqli("localhost", "username", "password", "database");

// Use the $connection variable whenever you need to interact with the database
$result = $connection->query("SELECT * FROM table_name");

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