What are the potential pitfalls of using mysql_ functions in PHP, and why is it recommended to switch to PDO?

Using mysql_ functions in PHP can lead to security vulnerabilities such as SQL injection attacks and lack of support for newer MySQL features. It is recommended to switch to PDO (PHP Data Objects) for database access as it provides a more secure and flexible way to interact with databases.

// Using PDO for connecting to a MySQL database
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}