Created
September 4, 2012 17:32
-
-
Save drakakisgeo/3623875 to your computer and use it in GitHub Desktop.
Function to mysql LOAD DATA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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