Jump to content

[SOLVED] MySQL query that will just not work, GEOIP


Pezmc

Recommended Posts

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?

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 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;
}

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();

 

PS, checkip works fine (tested with echo)

 

Link to comment
Share on other sites

To help debug it, try changing the get_country() function to this:

 

function get_country(){
    $user_ip = check_ip();
    $temp = explode('.',$user_ip);
    $ipnum = 16777216*$temp[0] + 65536*$temp[1] + 256*$temp[2] + $temp[3];
    $qry = "SELECT * FROM `geo_ip` WHERE '$ipnum' >= `begin_num` AND '$ipnum' <= `end_num`";
    echo "$qry<br /><br />"; // debugging code
    $qry = mysql_query($qry) or die(mysql_error());
    $get_country = mysql_fetch_array($qry) or die(mysql_error());
    print_r($get_country); // debugging code
    if ($get_country['country_name']=="") { return 'Unknown'; } else { return $get_country['country_name'].':'.$get_country['iso_code']; }
}

Link to comment
Share on other sites

$ipnum = 16777216*$temp[0] + 65536*$temp[1] + 256*$temp[2] + $temp[3];

 

 

could just be sprintf('%u', ip2long($user_ip())

 

 

 

I'm not sure where your problem lies, but I figure it has something to do with '$ipnum' >= `begin_num`.

 

There's no reason to put quotes around a number.  That could be making it be 0.

Link to comment
Share on other sites

To help debug it, try changing the get_country() function to this:

 

function get_country(){
    $user_ip = check_ip();
    $temp = explode('.',$user_ip);
    $ipnum = 16777216*$temp[0] + 65536*$temp[1] + 256*$temp[2] + $temp[3];
    $qry = "SELECT * FROM `geo_ip` WHERE '$ipnum' >= `begin_num` AND '$ipnum' <= `end_num`";
    echo "$qry<br /><br />"; // debugging code
    $qry = mysql_query($qry) or die(mysql_error());
    $get_country = mysql_fetch_array($qry) or die(mysql_error());
    print_r($get_country); // debugging code
    if ($get_country['country_name']=="") { return 'Unknown'; } else { return $get_country['country_name'].':'.$get_country['iso_code']; }
}

 

Doing this it printed/echoed:

SELECT * FROM `geo_ip` WHERE '1357240672' >= `begin_num` AND '1357240672' <= `end_num`

Array ( [0] => 34 [id] => 34 [1] => 7.57.75.64 [begin_ip] => 7.57.75.64 [2] => 9.19.255.255 [end_ip] => 9.19.255.255 [3] => 121195328 [begin_num] => 121195328 [4] => 152305663 [end_num] => 152305663 [5] => US [country] => US [6] => United States [name] => United States )

 

I don't see any problems there

 

$ipnum = 16777216*$temp[0] + 65536*$temp[1] + 256*$temp[2] + $temp[3];

could just be sprintf('%u', ip2long($user_ip())

I'm not sure where your problem lies, but I figure it has something to do with '$ipnum' >= `begin_num`.

There's no reason to put quotes around a number.  That could be making it be 0.

 

What is sprintf about? Never have heard of it!

Surely that can't be making the difference? I tried it without and there was no change apart from it appearing in a different order:

"SELECT * FROM `geo_ip` WHERE 1357240672 >= `begin_num` AND 1357240672 <= `end_num`

Array ( [0] => 24173 [id] => 24173 [1] => 80.229.0.0 [begin_ip] => 80.229.0.0 [2] => 80.229.255.255 [end_ip] => 80.229.255.255 [3] => 1357185024 [begin_num] => 1357185024 [4] => 1357250559 [end_num] => 1357250559 [5] => GB [country] => GB [6] => United Kingdom [name] => United Kingdom )"

 

Thanks to all people trying to help!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.