How can developers transition from using mysql_* functions to PDO in PHP for improved database connectivity?

To transition from using mysql_* functions to PDO in PHP for improved database connectivity, developers can rewrite their database queries using PDO prepared statements. This will help prevent SQL injection attacks and provide a more secure and flexible way to interact with the database.

// 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) {
    die('Connection failed: ' . $e->getMessage());
}

// Prepare and execute a query using PDO prepared statements
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$id = 1;
$stmt->execute();

// Fetch the results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process the results
}