Skip to content

Instantly share code, notes, and snippets.

@drakakisgeo
Created September 4, 2012 17:32
Show Gist options
  • Save drakakisgeo/3623875 to your computer and use it in GitHub Desktop.
Save drakakisgeo/3623875 to your computer and use it in GitHub Desktop.
Function to mysql LOAD DATA
function create_load_data_file($arrayofdata,$numcolumns,$pathtofile,$dbtable){
// Create the txt structure
$i = 0;
foreach($arrayofdata as $column){
$txtcontent .= $column."\t";
++$i;
if(($i % $numcolumns) == 0){ $txtcontent .= "\r\n";}
}
// Create the tmp txt file
$fh = fopen($pathtofile, 'w') or die("can't open file");
fwrite($fh, $txtcontent);
fclose($fh);
// add to DB
$query = "LOAD DATA LOCAL INFILE '".$pathtofile."' INTO TABLE ".$dbtable." LINES TERMINATED BY '\r\n'";
$query = @mysql_query($query)or die(mysql_error());
// Remove the tmp txt file
unlink($pathtofile);
if($query){
return TRUE;
}else{
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment