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

One potential pitfall of using the mysql_db_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 the mysqli or PDO extension for interacting with a MySQL database in PHP.

// Using mysqli extension to query a MySQL database
$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()) {
        // Process the data
    }
} else {
    echo "0 results";
}

$mysqli->close();