Why is it recommended to use PDO or mysqli instead of the MySQL extension in PHP, especially in newer versions of the language?
The MySQL extension in PHP is deprecated and has been removed in newer versions of the language. It is recommended to use PDO (PHP Data Objects) or mysqli (MySQL Improved) for database connectivity as they offer more features, better security, and support for multiple database systems.
// Using PDO for database connectivity
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';
try {
$pdo = new PDO($dsn, $username, $password);
// Perform database operations here
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}