Skip to content

Instantly share code, notes, and snippets.

@krikulis
Created June 30, 2011 15:58
Show Gist options
  • Save krikulis/1056535 to your computer and use it in GitHub Desktop.
Save krikulis/1056535 to your computer and use it in GitHub Desktop.
CSV => MySQL
<?php
$host = "";
$user = "";
$pass = "";
$file = ""
$link = mysql_connect($host, $user, $pass);
mysql_query("set names utf8;");
mysql_query("USE truevision");
$file = file_get_contents($file);
$file = explode("\n", $file);
function escape($var){
$var = trim($var, '"' );
return mysql_real_escape_string($var);
}
foreach($file as $line){
$line = explode(",", $line);
$q = "INSERT INTO ip2countries (`ip_from`, `ip_to`, `from`, `to`, `code`, `country`)
VALUES ('" . escape($line[0]). " ', '" . escape($line[1]) . "', '" . escape($line[2]) . "', '" . escape($line[3]) . "', '" . escape($line[4]) . "', '" . escape($line[5]) ."');";
$res = mysql_query($q);
if(!$res){
die(mysql_error());
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment