View Full Version : Importing a Multi-line CSV file into MySQL

12-18-2010, 11:44 PM
To start off, I'm using the following CSV file:


As you can see, the first "row" spans 3 lines. It's ok if the new lines in the "effects" column are replaced by "***" or whatever, I just need to preserve them when they are displayed back out.

1, Stench, 3, "This Pokémon's moves have approximately a 10% chance to make the target [flinch]{mechanic}.***This ability does not stack with a held [King's Rock]{item}.***Overworld: The wild encounter rate is halved while this Pokémon is in the party.", Has a chance of making the opponent [flinch]{mechanic} when attacking.

Also, each new row will begin with an integer, followed by a comma. If not, then it is a continuing line of the previous "effects" column.

id is an int(3)
name is a varchar(20)
generation_id is an int(1)
effect is a text
short_effect is a text

I already know the generic code to import the data from a CSV into MySQL, but the multiline thing just completely throws me off. Not sure where to go from here. A script to combine the lines into rows would be even more helpful to me, if anyone knows of something.

$handle = fopen("$filename", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$import="INSERT into info(spam,eggs) values('$data[0]','$data[1]')";
mysql_query($import) or die(mysql_error());

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum