fetching data from a csv to mysql database
Posted: Fri Apr 07, 2006 9:17 am
hi,
i have a problem when i was fetching data from a .csv file and inserting it into mysql database
the code is like:
the problem is that it's working fine when there are small data in the .csv file, but when the amount of data is huge(approx. 15000) it takes really long time to execute. how can i improve my code?
i have a problem when i was fetching data from a .csv file and inserting it into mysql database
the code is like:
Code: Select all
$handle = fopen ("test.csv","r");
$ignore_first_buffer = fgets($handle, 94096);
while (!feof($handle))
{
$line_num++;
$buffer = fgets($handle, 94096);
//echo $buffer;
$pieces = explode(",",$buffer);
$sql = "INSERT INTO `master` VALUES ( '', ";
for($i=0; $i <= count($pieces) - 2; $i++)
{
$sql .= "'$pieces[$i]',";
}
$sql .= "'$pieces[$i]' ); ";
if(!$pieces[0] && !$pieces[1] && !$pieces[2])
$sql = "";
print "$line_num -> ";
mysql_query($sql);
}
fclose($handle);