Juicy
Juicy

Reputation: 12510

geoip_country_by_name returns empty string, no errors

I've followed a tutorial to setup geoip. I've uploaded the files to my server and here is my code:

<?php
    include_once('geoip.inc');
    $gi = geoip_open('GeoIP.dat', GEOIP_STANDARD);

    include_once('sql_conn.php');

    $sql = "SELECT ip,vid FROM views";
    $result = mysqli_query($con, $sql);

    while ($row = mysqli_fetch_array($result)) {
        $ip = $row['ip'];
        $country = geoip_country_name_by_name($gi, $ip);
        echo $ip . " - " . $country . "<br />";
    } 

    geoip_close($gi);
?>

I'm trying to get the country by name by passing an IP address. My output is IE:

180.76.5.21 - 
162.222.182.156 - 
190.247.137.160 - 
190.247.137.160 - 
190.247.137.160 - 

I'm not getting any errors or warnings from PHP.

EDIT:

As pointed out in the comments, the PHP manual says geoip_country_name_by_name expects only one argument, but this is what happens when I pass only the $ip to it:

Warning: Missing argument 2 for geoip_country_name_by_name(), called in /home/rlcoachi/public_html/admin/update_db.php on line 12 and defined in /home/rlcoachi/public_html/admin/geoip.inc on line 448
90.221.27.198 - 

Upvotes: 1

Views: 1411

Answers (1)

Darren
Darren

Reputation: 13128

I think I've narrowed down the issue. You're trying to use geoip_country_name_by_name() which, via MaxMinds GeoIP Api expects a name (i.e a hostname). What you most likely want to try, is something like this:

while ($row = mysqli_fetch_array($result)) {
    $ip = $row['ip'];
    $country = geoip_country_name_by_addr($gi, $ip);
    echo $ip . " - " . $country . "<br />";
} 

Using get_country_name_by_addr() instead, as you want to pass the IP Address and not a host name.

Upvotes: 1

Related Questions