What are the potential errors that can occur when using the ipExists function in PHP to check for duplicate IPs?

When using the ipExists function in PHP to check for duplicate IPs, potential errors can occur if the function does not properly handle cases where the IP address is already present in the database. To solve this issue, you can modify the ipExists function to return a boolean value indicating whether the IP address exists in the database or not.

function ipExists($ip) {
    // Check if the IP address already exists in the database
    $query = "SELECT COUNT(*) FROM table_name WHERE ip_address = '$ip'";
    $result = mysqli_query($connection, $query);
    $count = mysqli_fetch_array($result)[0];

    // Return true if the IP address exists, false otherwise
    return $count > 0;
}