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 ;-)

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算