Page 1 of 1

fetching data from a csv to mysql database

Posted: Fri Apr 07, 2006 9:17 am
by jito
hi,
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);
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?

Posted: Fri Apr 07, 2006 9:20 am
by feyd
What's wrong with fgetcsv()?

some more help

Posted: Fri Apr 07, 2006 9:30 am
by jito
thnx
Feyd, actually i am new in this world of php, and know so little about all these functions available in php. Can u please tell me just another thing,from where can i get help learning socket programming using php?
thnx again.

Posted: Fri Apr 07, 2006 9:33 am
by feyd
via aol translation, jito wrote:Can you please tell me just another thing,from where can i get help learning socket programming using php?
I can't recommend anything for learning how to work with sockets to a newcomer. The only thing I can recommend is reading and understanding the protocol you are wanting to use.