What are the differences between using mysql functions, mysqli functions, and PDO in PHP for database interactions?
When interacting with a MySQL database in PHP, there are three main options for handling database interactions: mysql functions, mysqli functions, and PDO. - mysql functions are deprecated and should not be used in new projects due to security vulnerabilities and lack of support. - mysqli functions are an improved version of mysql functions with added features like support for prepared statements and transactions. - PDO (PHP Data Objects) is a database abstraction layer that supports multiple database types, making it more flexible and secure than mysql or mysqli functions.
// Using PDO for database interactions
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';
try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->bindParam(':id', $id);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    print_r($result);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}