In what ways can transitioning from using mysql_* functions to PDO in PHP improve code quality and security when working with databases?

Using PDO in PHP instead of mysql_* functions can improve code quality and security by providing a more secure and flexible way to interact with databases. PDO helps prevent SQL injection attacks by using prepared statements, allows for easier code maintenance and portability, and supports multiple database types without changing the codebase.

// Using PDO to connect to a MySQL database
$dsn = 'mysql:host=localhost;dbname=my_database';
$user = 'username';
$pass = 'password';

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