我正在编写从上传的CSV文件导入记录的wordpress插件。它应该如何工作:
-
从磁盘中选择文件;
-
点击上传;
-
脚本遍历文件并插入记录(列
postcode
为"唯一"),当存在postcode
时,将更新其他字段。
现在一些代码(针对此问题进行了简化):
///...
$handle = fopen($file, "r");
while( ($row = fgetcsv($handle)) !== FALSE ){
$searcher->wpdb->insert( $searcher->wpdb->prefix . "searcher_postcodes", array(
"name" => $row[1],
"postcode" => $row[0],
"state" => $row[2],
"state_short" => $row[3],
"city" => $row[4],
"lat" => $row[5],
"lng" => $row[6]
) );
echo "<br><code>" . $searcher->wpdb->last_query . "</code>";
if( $searcher->wpdb->insert_id > 0 ){
$imported++;
echo "success";
}else{
echo "failure";
}
}
几乎一切都很好,但有一次我得到了几张唱片:
导入 16348
INSERT INTO wp_searcher_postcodes (name, postcode, state, state_short, city, lat, lng) VALUES ('Marienwerder', '16348', 'Brandenburg', 'BB', 'Barnim', '52.85', '13.6')
成功导入 16352
INSERT INTO wp_searcher_postcodes (name, postcode, state, state_short, city, lat, lng) VALUES ('Marienwerder', '16348', 'Brandenburg', 'BB', 'Barnim', '52.85', '13.6')
故障
在第二个查询中,我有来自上一个查询的数据,但它失败了。所以不是每个。有没有办法排队或缓冲,或者类似的东西 wpdb 中的每个查询?
检查文件编码,我遇到了同样的问题,为我更改文件编码工作
我猜代码看起来不错。也许问题出在您的数据上。试试这个看看,如果行在csv中真的是唯一的:
echo "<br>" . $row[0] . ": <code>" . $searcher->wpdb->last_query . "</code>";