What is the recommended alternative to using the deprecated MySQL extension in PHP?
The recommended alternative to using the deprecated MySQL extension in PHP is to use either the MySQLi (MySQL Improved) extension or PDO (PHP Data Objects) extension. Both of these extensions provide more modern and secure ways to interact with MySQL databases in PHP.
// Using MySQLi extension
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}
// Using PDO extension
try {
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
Keywords
Related Questions
- How can the PHP code be optimized to handle larger datasets when generating XML files for download?
- How can you ensure that a specific output is displayed when no parameters are passed in a PHP script?
- Why does the connection setup take 10-15 seconds when using IMAP in PHP, and is there a way to speed up this process?