有一个名为:的mysql数据库example
,它有一个名为:的表products
,它有字段:products_model
和products_image
。模型字段是唯一的。
现在我有一个名为 .csv 的 csv 文件data.csv
。其中有两列 products_model 和products_image
.i 使用以下代码。但它不会将数据导入数据库。我的代码有什么问题?
$fp = fopen("data.csv","r");
$length = 4096; //have to be optional length
$fildDelineate = ',' ;//or "|" declare what you need
$dbhost="localhost";
$dbname="zencartone";
$dbuser="root";
$dbpass="";
$link = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname);
$databasetable = 'products';
$counter = 1;//to omission first row if it is table headers
while($line = fgetcsv($fp,$length,$fildDelineate)){
foreach($line as $key=>$value){
//for example insert in 3 column
$importSQL = "insert into $databasetable (products_model, products_image) values('$line[0]','$line[1]')";
mysql_query($importSQL) or die(mysql_error());
}
$counter++;
}
fclose($fp);
数据.csv:
有两列:
products_model products_image
MG200MMS dress/201247561673.jpg
MG400-32MB dress/2012471141673.jpg
MSIMPRO dress/201247741673.jpg
DVD-RPMK dress/2012471831673.jpg
DVD-BLDRNDC dress/2012474221673.jpg
DVD-MATR dress/201112132056773.jpg