PHP Classes

Import CSV files

Recommend this page to a friend!

      Top level forums  >  PHP Specialists  >  General  >  Import CSV files  
Subject:Import CSV files
Summary:import many csv files
Messages:3
Author:Gheorghe Sarbu
Date:2012-02-08 10:17:03
Update:2012-02-13 07:15:12
 

  1. Import CSV files   Reply   Report abuse  
Picture of Gheorghe Sarbu 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.




There are 2 replies in this thread, which are not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.