Skip to content
PHP Logo

What are the benefits of using MySQLi or PDO over the deprecated MySQL extension in PHP?

The deprecated MySQL extension in PHP has been removed in PHP 7.0 and higher, so it is recommended to use MySQLi (MySQL Improved) or PDO (PHP Data Objects) for database operations. MySQLi and PDO offer more features, better security, and support for prepared statements, making them more secure and efficient options for interacting with databases in PHP.

php
// Using MySQLi
$mysqli = new mysqli("localhost", "username", "password", "database_name");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Using PDO
$pdo = new PDO("mysql:host=localhost;dbname=database_name", "username", "password");

if (!$pdo) {
    die("Connection failed");
}