What are the advantages of using mysqli or PDO over mysql_-functions in PHP for database operations?

Using mysqli or PDO over mysql_-functions in PHP for database operations is recommended due to several advantages. Both mysqli and PDO offer improved security features such as prepared statements, which help prevent SQL injection attacks. They also provide support for multiple database systems, making it easier to switch between different databases without changing much of the code. Additionally, mysqli and PDO offer object-oriented interfaces, making it easier to work with databases in a more organized and efficient manner.

// Example using mysqli to connect to a database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// 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
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

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

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