The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
It is true that the MySQL extension is deprecated, and it is recommended to use either MySQLi or PDO instead.
The mysql extension was removed in PHP 7.0. It is strongly recommended to use either MySQLi or PDO instead.
MySQLi (MySQL Improved) is an enhanced extension for MySQL. It was developed to take advantage of new features found in MySQL server versions 4.1.3 and above. Unlike the original mysql extension, MySQLi supports both object-oriented and procedural interfaces.
// MySQLi (Object-Oriented)
$mysqli = new mysqli("localhost", "username", "password", "database");
$result = $mysqli->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
echo $row["name"];
}
$mysqli->close();PDO (PHP Data Objects) is a database access layer that provides a uniform method of access to multiple databases. It provides a data-access abstraction layer, which means that, regardless of which database you are using, you use the same functions to issue queries and fetch data. This makes it easier to switch between databases if your application requires it.
// PDO
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
$stmt = $pdo->query("SELECT * FROM users");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row["name"];
}Both MySQLi and PDO have their own benefits and drawbacks. MySQLi is optimized for MySQL and supports MySQL-specific features, while PDO offers better portability across different database systems. Which one you should choose depends on your specific needs and preferences.