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']; ?> Link to comment https://forums.phpfreaks.com/topic/174775-geo-location-by-ip/ 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 Link to comment https://forums.phpfreaks.com/topic/174775-geo-location-by-ip/#findComment-921091 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... Link to comment https://forums.phpfreaks.com/topic/174775-geo-location-by-ip/#findComment-921093 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. Link to comment https://forums.phpfreaks.com/topic/174775-geo-location-by-ip/#findComment-921096 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.