View Single Post
Old 06-22-2004, 11:10 AM   #1 (permalink)
jayteema
Registered User
 
Join Date: Jun 2004
Posts: 4
jayteema is on a distinguished road
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
jayteema is offline   Reply With Quote