Location: PHPKode > scripts > phpIP2Country > phpip2country/populate_database.php
<?php
/*
this file will populate the database
*/
include('database.class.php');
include('csv.class.php');

$db = new phpDB(array('type' => 'MySQL', 'host' => '127.0.0.1', 'username' => 'root', 'password' => '', 'database' => 'ip2country'));
$db->connect();
$db->selectDatabase();

$csv = new phpCSV();
$csv->open('ip-to-country.csv', 'r');

while(!$csv->attributes['eof']) {
	$result = $csv->fetch(1000);
	if($result <> false) {
		foreach($csv->attributes['csv']['result']['data'] as $row => $column) {
			$sql = "insert into ip2country (ip_from, ip_to, country_code2, country_code3, country_name) values (".$csv->getValue($row, 0).", ".$csv->getValue($row, 1).", ".sql_quote($csv->getValue($row, 2)).", ".sql_quote($csv->getValue($row, 3)).", ".sql_quote($csv->getValue($row, 4)).")";
			//print $sql."\n";
			$db->execute($sql);
		}
	}
}
$csv->close();

print "finished poplauting the IP2Country CSV data";

function sql_quote($value) {
	return "'".str_replace("'", "''", $value)."'";
}
?>
Return current item: phpIP2Country