Why is it recommended to use mysqli or PDO instead of the mysql API in PHP for database interactions?

It is recommended to use mysqli or PDO instead of the mysql API in PHP for database interactions because the mysql API is deprecated and no longer supported in newer versions of PHP. Both mysqli and PDO offer more features, better security, and support for prepared statements, making them more secure and flexible options for interacting with databases in PHP.

// Using mysqli to connect to a MySQL 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";