How can error handling be improved in the mysqli prepared statement?

When using mysqli prepared statements, error handling can be improved by checking for errors after executing the statement and displaying them if they occur. This can help in identifying and resolving any issues that may arise during the execution of the prepared statement.

$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

if ($stmt->execute()) {
    // Process the result
} else {
    echo "Error: " . $stmt->error;
}

$stmt->close();