What are the advantages of using MySQLi or PDO over the deprecated mysql_* functions in PHP when working with databases?

The deprecated mysql_* functions in PHP are no longer supported and pose security risks due to potential SQL injection vulnerabilities. It is recommended to use MySQLi (MySQL Improved) or PDO (PHP Data Objects) for interacting with databases in PHP as they offer more secure and flexible options for database operations.

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

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_name", "username", "password");