What are the key differences between mysqli and mysql extensions in PHP and how should they be used appropriately?
The key differences between mysqli and mysql extensions in PHP are that mysqli is the improved version of the mysql extension, offering better security features and support for prepared statements. It is recommended to use mysqli over mysql extension as mysql is deprecated in newer versions of PHP.
// Using mysqli extension in PHP
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if ($mysqli->connect_error) {
die('Connection failed: ' . $mysqli->connect_error);
}
// Perform queries using mysqli
$result = $mysqli->query("SELECT * FROM table");
// Close the connection
$mysqli->close();