Login   Register  
PHP Classes
elePHPant
Icontem

File: populate_database.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mike Leigh  >  phpIP2Country  >  populate_database.php  >  Download  
File: populate_database.php
Role: Auxiliary script
Content type: text/plain
Description: Script to populate the database with the CSV Data
Class: phpIP2Country
Determine the country of an IP in a database
Author: By
Last change:
Date: 2006-05-30 14:31
Size: 1,022 bytes
 

Contents

Class file image Download
<?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($row0).", ".$csv->getValue($row1).", ".sql_quote($csv->getValue($row2)).", ".sql_quote($csv->getValue($row3)).", ".sql_quote($csv->getValue($row4)).")";
            
//print $sql."\n";
            
$db->execute($sql);
        }
    }
}
$csv->close();

print 
"finished poplauting the IP2Country CSV data";

function 
sql_quote($value) {
    return 
"'".str_replace("'""''"$value)."'";
}
?>