|
csv file to mysql using php
I am using a very common script to insert a csv file
<?
# first get a mysql connection as per the FAQ
$DBhost = "localhost";
$DBuser = "DBuser";
$DBpass = "DBpass";
$DBName = "DBName";
$table = "table";
mysql_connect($DBhost,$DBuser,$DBpass,$DBName) or die("Unable to connect to database");
$fcontents = file ('./topscsv.csv');
# expects the csv file to be in the same dir as this script
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode(",", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','
$sql = "insert into $table values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
?>
It echos the data fine, but every other line says
No Database Selected
I am sure it is something stupid, but I can't by this issue
Thanks for your help
Jayteema
|