What are the advantages of using mysqli or PDO over mysql for database connections in PHP?

Using mysqli or PDO over mysql for database connections in PHP is recommended because mysql functions are deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. Both mysqli and PDO offer more secure ways to interact with databases, support prepared statements to prevent SQL injection attacks, and provide support for multiple database types. It is important to update your code to use mysqli or PDO to ensure compatibility and security.

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

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

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