What are the differences between using MySQL Extension, PDO, and MySQLi for database operations in PHP?
When working with databases in PHP, it is important to choose the right method for database operations. MySQL Extension is the oldest method and is now deprecated, so it is not recommended for new projects. PDO (PHP Data Objects) and MySQLi (MySQL Improved) are more modern and secure options for interacting with databases in PHP. PDO is a database abstraction layer that supports multiple database types, while MySQLi is specifically designed for MySQL databases and offers both procedural and object-oriented approaches.
// Using PDO for database operations in PHP
try {
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Perform database operations here
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}