How can the usage of mysqli or PDO improve the security and efficiency of PHP scripts compared to mysql_* functions?

Using mysqli or PDO instead of mysql_* functions can improve the security and efficiency of PHP scripts by providing prepared statements that help prevent SQL injection attacks. These newer database extensions also support more modern features and better error handling, making it easier to write secure and optimized code.

// Using mysqli to connect to a database
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Using PDO to connect to a database
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);