What is the impact of using mysql_close() prematurely in PHP scripts, and how can it lead to errors like "No database selected"?

Prematurely using mysql_close() in PHP scripts can lead to errors like "No database selected" because it closes the connection to the database before all queries have been executed. To solve this issue, ensure that all database operations are completed before closing the connection.

// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Perform database operations

// Close the connection only after all operations are completed
mysqli_close($connection);