What is the difference between the mysqli and mysql extensions in PHP?
The main difference between the mysqli and mysql extensions in PHP is that mysqli is the improved version of mysql and provides more features and enhanced security. It supports prepared statements, transactions, and stored procedures, making it more robust and secure compared to mysql. It is recommended to use mysqli over mysql for new projects.
// Using mysqli extension instead of mysql extension
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}
// Use $mysqli object for database operations