What potential pitfalls are associated with using the mysql_query function in PHP for database operations?

One potential pitfall of using the mysql_query function in PHP for database operations is that it is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. This means that it is no longer supported and may lead to security vulnerabilities and compatibility issues. To address this, it is recommended to use MySQLi or PDO extensions for interacting with databases in PHP.

// Using MySQLi extension to perform database operations
$mysqli = new mysqli("localhost", "username", "password", "database");

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

$query = "SELECT * FROM table";
$result = $mysqli->query($query);

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

$mysqli->close();