在上一次传递

时间:2016-01-01 23:36:04

标签: php mysql

这个例程过去一直工作,直到我们转移到速度更快的debian linux服务器。 这是一段代码片段,它通过csv文件读取,如果存在具有相同manufacturer_name dosn的其他记录,则将记录插入表中。发生的事情是插入第一条记录,当找到另一条记录时,执行功能无法找到先前插入的记录并添加记录而不是更新。如果我第二次运行相同的例程而没有清空表,则找到所有记录并且仅进行更新。我想也许问题是速度所以我试着把睡眠放在过程中,但它没有任何影响。有任何想法吗? 感谢

while (($data = fgetcsv($handle, 0, chr(9),chr(0))) !== FALSE) {

    ****** Setup stuff here  *******
    $msql_data_array = array('manufacturers_name' => $data[$manufacturer_sn]);
    $sql = "select count(*) as total,manufacturers_id,manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_name = \"" . $data[$manufacturer_sn] . "\"";

    $manufacturers = $db->Execute($sql);
    if ($manufacturers->fields['total'] == 0) {  <<<<<<<<<<<< This always returns 0 even if the record was just added by the previous operation(s)
            ***** Inserts a new record ******

    } else {
            ***** Updates the current record *******
    }

]

1 个答案:

答案 0 :(得分:0)

这看起来像一个简单的 .csv 导入。您可以使用命令行MySQL调用替换PHP代码,如下所示:

  

mysqlimport --fields-terminated-by&#39; \ t&#39; db_name import.csv

参考:http://dev.mysql.com/doc/refman/5.7/en/mysqlimport.html

相关问题