Page 1 of 1

CSV into mysql?

Posted: Sat May 17, 2003 2:05 pm
by CONFIQ
Let's say i have file like this:

Code: Select all

"3230826240","3230827007","US","UNITED STATES"
"3230827520","3230827775","AT","AUSTRIA"
"3230827776","3230828031","NZ","NEW ZEALAND"
"3230828032","3230828543","HU","HUNGARY"
"3230828544","3230830079","GB","UNITED KINGDOM"
And i want to put it in mysql, but i can't use "load file" becaouse phpmyadmin shows me "The used command is not allowed with this MySQL version (4.0.12)"

Does anyone have a clew how to solve this?

Posted: Sun May 18, 2003 5:29 pm
by patrikG

Code: Select all

<?php
	$fd = fopen("location of your textfile", "r");
	while($content=fgets($fd,1024))
		{
list($var1,$var2,$countryAbbreviation,$countryName)=explode(",",$content);
$sql=mysql_query("INSERT INTO ... (var1, var2, countryAbbreviation, countryName) VALUES ($var1,$var2,$countryAbbreviation,$countryName);");
}
?>
I haven't run the script - but bascially: the file is read line-by-line, then split by "," and assigned to $var1,$var2,$countryAbbreviation,$countryName. These variables are then put into the DB.

Posted: Mon May 19, 2003 10:53 am
by CONFIQ
Hey, that's verry smart :)
I was thinking to do it with Regular expressions but now i see i can do it with list() 2 :)

I'm on the way 2 do it ;)