How can the use of the mysqli class in PHP improve database connection handling compared to the provided script?

The use of the mysqli class in PHP can improve database connection handling by providing a more secure and efficient way to interact with MySQL databases. It offers features like prepared statements, parameterized queries, and improved error handling, which can help prevent SQL injection attacks and streamline database operations.

// Using mysqli class for improved database connection handling

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Perform SQL query
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();