您好我正在尝试编写一个脚本,根据其名称将csv加载到不同的表中。
例如,Brooklyn Nets 24-01-2015.csv或Brooklyn Nets 28-01-2015.csv应加载到mysql数据库的Brooklyn Nets表中,但应跳过Boston Celtics 24-01-2015.csv。 CSV字段与表格匹配,因此无需映射。
我不知道从哪里开始。有没有人有任何指示?
答案 0 :(得分:0)
此处您有关于如何导入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);
?>
(来源:http://www.infotuts.com/import-csv-file-data-in-mysql-php/)
关于如何从文件名中获取表名,当您获取上传的文件时尝试解析名称,您将获得该表:
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name]; <-- // Parse this
...
}