What are the advantages of using PDO with prepared statements over the mysql_ functions in PHP?

Using PDO with prepared statements is advantageous over the mysql_ functions in PHP because it provides a more secure way of interacting with databases by preventing SQL injection attacks. Prepared statements separate the SQL query from the user input, making it impossible for malicious input to alter the query structure. Additionally, PDO is more versatile as it supports multiple database systems, making it easier to switch between different databases in the future.

// Using PDO with prepared statements to insert data into a database

// Connect to the database using PDO
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a SQL query with a placeholder for the user input
$stmt = $pdo->prepare('INSERT INTO users (username, email) VALUES (:username, :email)');

// Bind the user input to the placeholders
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);

// Set the user input values
$username = 'john_doe';
$email = 'john.doe@example.com';

// Execute the prepared statement
$stmt->execute();