CSV into mysql?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

CSV into mysql?

Post 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?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
CONFIQ
Forum Commoner
Posts: 32
Joined: Fri Oct 18, 2002 3:39 pm
Location: Israel - Raanana

Post 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 ;)
Post Reply