Hi I'm trying to write a script that will load csv's to different tables depending on their name.
For example Brooklyn Nets 24-01-2015.csv or Brooklyn Nets 28-01-2015.csv should be loaded to the Brooklyn Nets table on the mysql database but Boston Celtics 24-01-2015.csv should be skipped. The CSV fields match up with the table so there is no need for mapping.
I have no idea where to start. Does anyone have any pointers?
Here you have complete example about how to import CSV
<?php
$connect = mysql_connect('localhost','root','12345');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
$cid =mysql_select_db('test',$connect);
// supply your database name
define('CSV_PATH','C:/wamp/www/csvfile/');
// path where your CSV file is located
$csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['ID'] = $csv_array[0];
$insert_csv['name'] = $csv_array[1];
$insert_csv['email'] = $csv_array[2];
$query = "INSERT INTO csvdata(ID,name,email)
VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
$n=mysql_query($query, $connect );
$i++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysql_close($connect);
?>
(source: http://www.infotuts.com/import-csv-file-data-in-mysql-php/ )
About how to get table name from file name, when you get the uploaded file try to parse name and you will get the table:
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name]; <-- // Parse this
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.