fetching data from a csv to mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

fetching data from a csv to mysql database

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What's wrong with fgetcsv()?
jito
Forum Commoner
Posts: 85
Joined: Sat Mar 25, 2006 4:32 am
Location: india

some more help

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply