gerkintrigg Posted September 19, 2009 Share Posted September 19, 2009 Hello. I downloaded the Max Mind database and am trying to use a simple PHP MySQL lookup to find the location of an IP address. I think I may be doing something wrong with the code because it keeps coming up with "United States" even though I'm testing it with UK IPs. Here's my code: <?php $root=''; //include the database: include $root.'includes/db.php'; // IP address $code = '81.17.79.2'; //get where the IP relates to: $q="SELECT `friendly_country` FROM `geo_locator` WHERE '$code' BETWEEN `ip_1` AND `ip_2`;"; $sql=mysql_query($q); $r=mysql_fetch_array($sql); echo 'IP '. $code. " mapped to: " . $r['friendly_country']; ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted September 19, 2009 Share Posted September 19, 2009 I'm guessing that ip_1 and ip_2 are in plain integer format? Can we see some sample row data? Also, if it is in plain integer format, you should convert $code to an integer before. ip2long Or in MySQL INET_ATON Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted September 19, 2009 Author Share Posted September 19, 2009 in the database it's in a varchar... Quote Link to comment Share on other sites More sharing options...
corbin Posted September 19, 2009 Share Posted September 19, 2009 BETWEEN works on numbers, not strings. xxx.xxx.xxx.xxx doesn't compare well as a number. I suggest storing the IPs as an unsigned integer or a long, then BETWEEN would work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.