How can the use of mysql_query() function in the code snippet be improved for better error handling?

The issue with the current code snippet is that it is using the deprecated `mysql_query()` function without proper error handling. To improve error handling, you should switch to using `mysqli_query()` or PDO with prepared statements and error handling. This will allow you to catch and handle any errors that may occur during the query execution.

// Improved code snippet using mysqli_query() with error handling

// Establish a database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Perform a query with error handling
$query = "SELECT * FROM table";
$result = $mysqli->query($query);

if (!$result) {
    die("Error executing query: " . $mysqli->error);
}

// Process the query result
while ($row = $result->fetch_assoc()) {
    // Process each row
}

// Close the connection
$mysqli->close();