Why is it important to transition from mysql_* functions to PDO for database interactions in PHP, and how can this transition improve the script's security and efficiency?

It is important to transition from mysql_* functions to PDO for database interactions in PHP because mysql_* functions are deprecated and insecure, making scripts vulnerable to SQL injection attacks. PDO provides a more secure and efficient way to interact with databases by using prepared statements and parameterized queries.

// Using PDO for database interactions
try {
    $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}