Page 1 of 1
CSV file uploading with thousands of records
Posted: Wed Aug 26, 2009 4:22 am
by pbs
Hi All,
Is there any script by which I can upload CSV file with thousands of records in to mysql database.
Thanks
Re: CSV file uploading with thousands of records
Posted: Wed Aug 26, 2009 4:23 am
by Mark Baker
phpmyadmin?
Re: CSV file uploading with thousands of records
Posted: Wed Aug 26, 2009 4:26 am
by pbs
Thanks for you reply, but I want PHP code to do that.
Re: CSV file uploading with thousands of records
Posted: Wed Aug 26, 2009 4:31 am
by Mark Baker
pbs wrote:Thanks for you reply, but I want PHP code to do that.
phpmyadmin?
Re: CSV file uploading with thousands of records
Posted: Wed Aug 26, 2009 3:57 pm
by Darhazer
offtopic:
you can use load data infile for that
As for the php script, without knowing the table name and column names... does the CSV file include column names as first line? Should be there any mapping?
Re: CSV file uploading with thousands of records
Posted: Thu Aug 27, 2009 1:23 am
by woahtuff
This is the script I use to upload a .csv into mysql. I haven't used it in a while, but I believe the column names in the .csv need to match the fields in the table. Replace 'username', 'password', 'your_db', 'your_file.csv' and 'your_table' with your values. I've uploaded about 2500 records into my db at once this way.
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("your_db", $con);
$fcontents = file ('./your_file.csv');
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i], ',');
$arr = explode(',', $line);
$sql = 'INSERT INTO your_table VALUES ("' . implode('","', $arr) . '")';
mysql_query($sql);
echo $sql .'<br>';
if(mysql_error()) {
echo mysql_error() .'<br>';
}
}
mysql_close($con);