Location: PHPKode > projects > Statiqz, website statistics > statiqz/ext/recalcip.php
<?php
// this is some very ugly code :) but it does recalculate all ip2countries.
// its recommended to run this if you install a new ip2country table.

    echo 'recalculate IPs <p>';

    $rid = mysql_connect("localhost", "username", "password");

    mysql_select_db("statiqz");

    $result = mysql_query("SELECT * FROM `visitors`;", $rid);

    while ($buffer = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $ip = sprintf("%u",ip2long($buffer['ip'])).'<br>';
        $id = $buffer['ID'];

        $cid = getcountry($ip).'<br>';

        
        $quer = mysql_query("UPDATE visitors SET countryID='$cid' WHERE ID='$id';");
    }

    FUNCTION GetCountry($ip){
        $queryresult = MySQL_Query("SELECT * FROM ip2country WHERE lower < '$ip' AND upper > '$ip'");


        IF ($fetchresult=MySQL_Fetch_Array($queryresult)){
                $temp=$fetchresult['code2'];

                $res=MySQL_Query("SELECT * FROM countries WHERE code = '$temp'");

                if ($fet=mysql_fetch_array($res)) {
                    $country = $fet['ID'];
                }
        }ELSE{
                $country=false;
        }

        RETURN $country;    
    }
?>
Return current item: Statiqz, website statistics