MySQL query that will just not work, GEOIP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

MySQL query that will just not work, GEOIP

Post by Pezmc »

Here is a little script that I CANNOT get to work! It is crazy!
It is a problem with the mysql_fetch_array but I can't understand why. When the page is visited you see: Unknown and no errors at all.

When I run "SELECT * FROM `geo_ip` WHERE '$ipnum' >= `begin_num` AND '$ipnum' <= `end_num`" manually from PhpMyAdmin with my $ipnum it works fine and outputs two rows.

I don't understand why this script doesn't work at all?
I am using the geo_ip database.

Does anyone know what is going on?

Code: Select all

 
function connect_me(){
    $DB_USER =  'REMOVED';
    $DB_PASSWORD = 'REMOVED';
    $DB_HOST =  'REMOVED';
    $dbc = mysql_connect ($DB_HOST, $DB_USER, $DB_PASSWORD) or die(mysql_error());
    mysql_select_db('REMOVED') or die(mysql_error());
    mysql_query("SET NAMES `utf8`") or die(mysql_error());
    if($ERROR_REPORTING == 1){ do_error($error,2);} 
}
 
function disconnect_me(){
    @mysql_close($dbc);
    @mysql_close();
}
 
function get_country(){
    $user_ip = check_ip();
    $temp = explode('.',$user_ip);
    $ipnum = 16777216*$temp[0] + 65536*$temp[1] + 256*$temp[2] + $temp[3];
    $qry = mysql_query("SELECT * FROM `geo_ip` WHERE '$ipnum' >= `begin_num` AND '$ipnum' <= `end_num`") or die(mysql_error());
    $get_country = mysql_fetch_array($qry) or die(mysql_error());
    if ($get_country['country_name']=="") { return 'Unknown'; } else { return $get_country['country_name'].':'.$get_country['iso_code']; }
}
 
$country_info = explode(':',get_country());
 
echo $country_info;
disconnect_me();
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: MySQL query that will just not work, GEOIP

Post by requinix »

Where are you getting check_ip() from?

When you tested the query yourself, did you figure out $ipnum yourself? If not, print it out ($ipnum or the query) and try that.
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Re: MySQL query that will just not work, GEOIP

Post by Pezmc »

I used the function to calculate it (with an echo) and just pasted that into the manual query
Check ip is:

Code: Select all

function check_ip(){
    $ipParts = explode(".", $_SERVER["REMOTE_ADDR"]);
    if ($ipParts[0] == "165" && $ipParts[1] == "21") {    
        if (getenv("HTTP_CLIENT_IP")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        } elseif (getenv("REMOTE_ADDR")) {
            $ip = getenv("REMOTE_ADDR");
        }
    } else {
       return $_SERVER["REMOTE_ADDR"];
    }
    return $ip;
}
But that is all fully working.

PS thanks for trying to help!
Post Reply