What are the key differences between MySQL and MySQLi in PHP?

The key differences between MySQL and MySQLi in PHP are that MySQLi is an improved extension of MySQL, offering better security features, support for prepared statements, and object-oriented interface. It is recommended to use MySQLi over MySQL for new projects due to its enhanced functionality and improved performance.

// Using MySQLi in PHP
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

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

// Perform SQL queries using MySQLi
$result = $mysqli->query("SELECT * FROM table");

// Close the connection
$mysqli->close();