How can PHP developers transition from using mysql_* functions to more modern and secure alternatives like PDO?

The mysql_* functions in PHP are deprecated and insecure, and developers should transition to using modern alternatives like PDO for database operations. To make this transition, developers can rewrite their database queries using PDO prepared statements, which provide better security by preventing SQL injection attacks.

// Connect to the database using PDO
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

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

// Prepare and execute a query using PDO prepared statements
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute(['email' => $email]);

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($results as $row) {
    echo $row['username'] . '<br>';
}