PHP Classes

File: populate_database.php

Recommend this page to a friend!
  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: 16 years ago
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($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)."'";
}
?>

For more information send a message to info at phpclasses dot org.