What are the differences between using MySQL and MySQLi in PHP for database operations?

When performing database operations in PHP, it's important to choose the appropriate MySQL extension. MySQLi is the improved version of MySQL, offering more features and better performance. It supports prepared statements, transactions, and stored procedures, making it a more secure and efficient choice for interacting with MySQL databases in PHP.

// Using MySQLi for database operations in PHP
$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);
}

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

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

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