donglian4464 2013-01-25 20:19
浏览 97
已采纳

在读取第一行后,使用fgetcsv获取额外的逗号和引号

I'm trying to import a large csv file that has 100,000 rows into a MySql table. I'm getting the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Dell', '', '1610 Keeneland Dr', '', 'Helena', '205-871-8006', '', '35080-4114')' at line 1

changing the code from a query to an echo:

'428A1B35-6913-497C-907A-BFB9C4983B04', 'Andrew Collins O'Dell', '', '1610 Keeneland Dr', '', 'Helena', '205-871-8006', '', '35080-4114')

you can see that there is extra quotes (2 single ticks) and a comma between some fields. i thought maybe it was an empty field but no dice. here is my code:

if (($handle = fopen("accounts.csv", "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, ",")) !== FALSE)
    {
        try
        {
            mysql_query("INSERT INTO accounts (id, name
            ,billing_address_street
            , billing_address_city, billing_address_state, billing_address_postalcode
            , phone_office, phone_alternate
            , website) values ('".trim(com_create_Guid(), '{}')
            ."', '".$data[1]."', '".$data[46]."', '".$data[48]
            ."', '".$data[49]."', '".$data[50]."', '".$data[8]
            ."', '".$data[9]."', '".$data[52]."')")or die(mysql_error());
        }
        catch(MySqlException $e)
        {
            $e.getMessage();
        }
    }
    fclose($handle);
}

i have tried putting in the field enclosure paramenter '"' in the fgetcsv function. i get an undefined offset for some of my indexes. so the question is if anyone has any idea what the extra ticks and commas could be coming from.

side note: it is inserting 250 records although everything after the name is off by a column or 2 which is of course because of the extra ticks and commas.

  • 写回答

1条回答 默认 最新

  • douyong1285 2013-01-25 20:34
    关注

    you have to use the function

    mysql_real_escape_string( $data[$x], $dblink )
    

    for the data parts ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效