What are the differences between using MYSQL and MYSQLI for database operations in PHP?

When working with databases in PHP, there are two main options for interacting with MySQL databases: MYSQL and MYSQLI. MYSQLI (MySQL Improved) is the newer and recommended method as it provides a more secure and feature-rich interface compared to the older MYSQL extension. MYSQLI supports prepared statements, transactions, and more advanced features, making it a better choice for modern PHP applications.

// Using MYSQLI for database operations in PHP
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

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

// Perform database operations using MYSQLI methods
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();