Question

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.

Was it helpful?

Solution

you have to use the function

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

for the data parts ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top