What are the benefits of switching from mysql_* functions to mysqli or pdo in PHP code?

Switching from mysql_* functions to mysqli or PDO in PHP code is important because the mysql_* functions are deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. Using mysqli or PDO provides better security through prepared statements, support for transactions, and improved error handling. It also allows for better compatibility with newer versions of PHP.

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

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

// Using PDO
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);