What are some potential pitfalls of using the mysql_query function in PHP and how can they be avoided?

One potential pitfall of using the mysql_query function in PHP is that it is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. To avoid this issue, it is recommended to use MySQLi or PDO extensions for interacting with MySQL databases in PHP.

// Using MySQLi extension to connect to a MySQL database and perform a query
$mysqli = new mysqli("localhost", "username", "password", "database");

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

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

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Process each row
    }
} else {
    echo "0 results";
}

$mysqli->close();