How can using mysqli instead of mysql improve the security and efficiency of database operations in PHP?

Using mysqli instead of mysql improves security and efficiency in PHP database operations because mysqli supports prepared statements, which help prevent SQL injection attacks by separating SQL logic from user input. Additionally, mysqli has better error handling and supports transactions, making it more reliable for database operations.

// Connect to database using mysqli
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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