Gheorghe Sarbu - 2012-02-08 10:44:05
Hello !
I want to import multiple CSV files at the same time, in a MySQL database.
Table name created will be include the file names.
My code:
<?php
$hostname="localhost";
$username="root";
$password="";
$database="test";
$conexiune=mysql_connect($hostname,$username,$password)
or die ("can't connect to database");
$bazadate=mysql_select_db($database,$conexiune)
or die ("can't find database");
$folder=dir("./folder");
while($folderEntry=$folder->read())
{
$info = pathinfo($folderEntry);
$file_name = basename($folderEntry,'.'.$info['extension']);
//echo $file_name."<br>";
$query = "DROP TABLE IF EXISTS '.$file_name.'; ";
$query .= "CREATE TABLE IF NOT EXISTS '.$file_name.' (name varchar(20), city varchar(50), id int);";
$query .= "load data local infile 'C:/xampp/htdocs/folder/'.$file_name.csv' into table '.$file_name.' fields terminated by \",\" enclosed by '"' lines terminated by \'\r\n\'";
mysql_query($query, $conexiune);
}
$folder->close();
mysql_close($conexiune);
?>
The error is on line 26.
Please help me.
Thank you very much in advance.