Using the MaxMind geoip database - Part 4 - Testing the longitude and latitudes

Just a simple bit of code this, almost an evolution of the previous test program. It looks up your own ip address and spits out the longitude and latitude of your country. On with the code!

<?php
/*
 * test_geoip.php - Test of the geoip database
 * - Chris Andrews (http://www.toxicbyte.com/)
 *
 * History:
 * 01/09/2006
 *  - Not very complicated this.
 */

$db_host = "xxxxxx";
$db_user = "xxxxxx";
$db_pass = "xxxxxx";
$db_name = "xxxxxx";

$sql_select_country_from_ip = <<<SQL
SELECT  *
FROM    geo_ip, country_coords
WHERE   begin_num <= %s AND end_num >= %s
AND     geo_ip.country_code = country_coords.country_code;
SQL;

$dbh = mysql_connect($db_host, $db_user, $db_pass)
    or die(‘I cannot connect to the database because: ‘ . mysql_error());

mysql_select_db($db_name);
?>
Testing Database:<br />
<?php

$remote_ip = mysql_real_escape_string($_SERVER[‘REMOTE_ADDR’]);
$remote_ip_as_long = sprintf("%u", ip2long($remote_ip));

print "Your IP is: $remote_ip <br />n";
print "This as a long is: $remote_ip_as_long <br />n";

if (! $result = mysql_query(sprintf($sql_select_country_from_ip,
                      $remote_ip_as_long,
                      $remote_ip_as_long)))
{
    print "No way pedro";
    exit;
}
else
{
    if ($row = mysql_fetch_assoc($result))
    {
        print "<br />";
        print "<br />";
        print $row["country_code"];
        print "<br />";
        print $row["country_name"];
        print "<br />";
        print $row["latitude"];
        print "<br />";
        print $row["longitude"];
        print "<br />";
    }
    else
    {
        print "-1";
    }
}

?>