What are the benefits of using mysqli_ or PDO over the deprecated mysql_ functions in PHP?

The mysql_ functions in PHP are deprecated and no longer recommended for use due to security vulnerabilities and lack of support for modern database features. It is recommended to use either the mysqli_ extension or PDO (PHP Data Objects) for interacting with databases in PHP. Both mysqli_ and PDO offer improved security, support for prepared statements, and compatibility with multiple database systems.

// Using mysqli to connect to a database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Using PDO to connect to a database
$dsn = "mysql:host=localhost;dbname=database";
$username = "username";
$password = "password";

try {
    $conn = new PDO($dsn, $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}