What are the advantages of using MySQLi or PDO over the mysql_* API in PHP for database queries?

Using MySQLi or PDO over the mysql_* API in PHP for database queries is recommended because they offer improved security features such as prepared statements to prevent SQL injection attacks. Additionally, MySQLi and PDO provide support for multiple database systems, making it easier to switch between databases without changing your code. They also offer object-oriented interfaces, making it easier to work with databases in a more structured and efficient manner.

// Using MySQLi to connect to a database
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";