What are the benefits of using mysqli or PDO over the deprecated mysql_* extension in PHP for database operations?
The mysql_* extension in PHP is deprecated and should not be used for database operations due to security vulnerabilities and lack of support for modern database features. It is recommended to use either the mysqli extension or PDO (PHP Data Objects) for database operations as they provide better security, support for prepared statements, and overall improved performance.
// Using mysqli extension
$mysqli = new mysqli("localhost", "username", "password", "database_name");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Using PDO
try {
$pdo = new PDO("mysql:host=localhost;dbname=database_name", "username", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}