Web Hosting Info

Search:

featured partner

The IP to Country Database

  Forum Topics : Development / Simple CSV to MySql Php
Submitted by Quezacotl on Thu, 09/16/2004 - 07:17.
Change .csv to .txt with Microsoft Excel

==============================
Create Mysql Table
==============================
CREATE TABLE iptocountry (
ip_from varchar(12) NOT NULL default '',
ip_to varchar(12) NOT NULL default '',
country_code2 char(2) default NULL,
country_code3 char(3) default NULL,
country_name varchar(50) default NULL
) TYPE=MyISAM;

==============================
Make a file example upload.php
==============================

$mysql_host = 'localhost';
$mysql_username = 'username';
$mysql_password = 'password';
$mysql_dbName = 'database';
$dbh = mysql_connect("$mysql_host", "$mysql_username", "$mysql_password");
mysql_select_db("$mysql_dbName");

$fl="ip-to-country.txt";
$fp=fopen($fl, "r");
$fr=fread($fp, filesize($fl));
$line=explode("\n", $fr);
$fno = 0;
for($i=1; $i $lin=explode(" ", $line[$i]);
mysql_query("INSERT INTO iptocountry (ip_from,ip_to,country_code2,country_code3,country_name) VALUES ('$lin[0]','$lin[1]','$lin[2]','$lin[3]','$lin[4]')");
}
mysql_close($dbh);

?>

========================
Demo site http://iptocountry.mallam.com
========================

Comment viewing options:
Select your preferred way to display the comments and click 'Save settings' to submit your changes.
Import CVS to SQL
Posted by sjenny on Sun, 10/03/2004 - 09:06.
// PHP TAG COME HERE
################################################################################
//
// by https://buysellpay.com
// please if using our script, give us credit or a link back
//
################################################################################

// READ THIS BEFORE YOU START

// proceed witht the modification of this file to match your needs
// USERNAME, PASSWORD, DATABASE, TABLE_NAME
// upload this (.php) file to your server

// open the csv file and replace ' with a space
// COTE D'IVOIRE and by LAO PEOPLE'S DEMOCRATIC REPUBLIC
// this sign may create a bug during file insertion into the SQL database
// total lines are around 30, you can modify them back later is you need
// upload ip-to-country.csv into your server
// ip-to-country.csv must be in the same directory as this php file

// create the database and the table to insert your data
// if the table is not yet available check out below for a model of the table

// browse to this .php file to run the script
// when the results is done, remove this .php file from the server

// That's it you are done

################################################################################

// TABLE

// create the table using SQL QUERY from PHPmyAdmin
// MODIFY THE NAME OF THE TABLE
// unquote the query
//CREATE TABLE `NAME_OF_THE_TABLE_HERE` (
// `id` int(25) NOT NULL default '0',
// `ipfrom` double(15,0) NOT NULL default '0',
// `ipto` double(15,0) NOT NULL default '0',
// `country2` char(2) NOT NULL default '',
// `country3` char(3) NOT NULL default '',
// `country` varchar(35) NOT NULL default ''
//) TYPE=MyISAM;

################################################################################

// SCRIPT TO IMPORT CVS TO SQL

// connect to the database
// MODIFY USERNAME AND PASSWORD
// user_name_here = user account name
// pass_word_here = password to user acc
$link = mysql_connect("localhost", "user_name_here", "pass_word_here") or die("Could not connect: ".mysql_error());
// MODIFY DATABASE NAME
// database_name_here = database name
// name of the table as defined in your database
$db = mysql_select_db("database_name_here") or die(mysql_error());
// débute - start
$row = 1;
// ouvre en lecture le fichier csv - read the csv file
$handle = fopen ("ip-to-country.csv","r");
// prend la valeur, max 1000 par ligne - max value per line 1000
while ($data = fgetcsv ($handle, 1000, ",")) {
// insertion dans la table - insert into the table
// MODIFY THE NAME OF THE TABLE
$query = "INSERT INTO name_of_table_here(`id`, `ipfrom`, `ipto`, `country2`, `country3`, `country`) VALUES('".
$row."', '".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."')";
$result = mysql_query($query) or die("Invalid query: " . mysql_error().__LINE__.__FILE__);
// prochaine ligne - next row
$row++;
}
fclose ($handle);
// ferme le fichier csv - close csv file
// Affiche le résultat - print result
echo "done - delete this file from your server";
echo "exécuté - supprimer ce fichier de votre serveur";

?>
################################################################################

// MORE IP TO COUNTRY FROM GEOIP COUNTRY LIST

// download the list of ip by country from:
// http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
// unzip and open it in text format
// remplace the ' with a space by IVORY en LAOS

// modified script to import this csv into sql is below
// the country_code 3 is not available
// the pointed ip are not imported, change as per your needs


################################################################################

// SCRIPT TO IMPORT GEOIP .CVS TO SQL

// ADD PHP TAG
// connect to the database
// MODIFY USERNAME AND PASSWORD
// user_name_here = user account name
// pass_word_here = password to user acc
$link = mysql_connect("localhost", "user_name_here", "pass_word_here") or die("Could not connect: ".mysql_error());
// MODIFY DATABASE NAME
// database_name_here = database name
// name of the table as defined in your database
$db = mysql_select_db("database_name_here") or die(mysql_error());
// débute - start
$row = 1;
// ouvre en lecture le fichier csv - read the csv file
$handle = fopen ("ip-to-country.csv","r");
// prend la valeur, max 1000 par ligne - max value per line 1000
while ($data = fgetcsv ($handle, 1000, ",")) {
// insertion dans la table - insert into the table
// MODIFY THE NAME OF THE TABLE
$query = "INSERT INTO name_of_the_table_here(`id`, `ipfrom`, `ipto`, `country2`, `country`) VALUES('".
$row."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."')";
$result = mysql_query($query) or die("Invalid query: " . mysql_error().__LINE__.__FILE__);
// inserted only the row number, ipfrom, ipto, country_code2 and country_name
// prochaine ligne - next row
$row++;
}
fclose ($handle);
// ferme le fichier csv - close csv file
// Affiche le résultat - print result
echo "done - delete this file from your server";
echo "exécuté - supprimer ce fichier de votre serveur";

?>
################################################################################
 
Also, remove something else
Posted by Lordo on Tue, 10/12/2004 - 23:51.
Your way worked greate for me, sjenny. Thanks. But I had to replace every " with blank so that it works. Otherwise, it does not store anything in the database.