0

This is my first time trying to import data from csv file into mysql table.

The problem is that the order of csv columns doesn't match the order in table and also there is one column (auto increment) id missing from csv. I am trying to do this but it gives me syntax error.

        $sql="LOAD DATA INFILE '$file' 
 INTO TABLE calls 
 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' 
 OPTIONALLY ENCLOSED BY ".'"'." 
 (`account_code`,`source`,`destination`,`dbcontext`,`caller_id`,`channel`,`destination-channel`,`lastapp`,`lastdata`,`start`,`answer`,`end`,`duration`,`billseconds`,`disposition`,`amaflag`,`call_id`,`userfield`)";

Notice the $file is the file name dynamically generated. I get this error upon execution of the query.

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 'OPTIONALLY ENCLOSED BY " (`account_code`,`source`,`destination`,`dbcontext`,`' at line 4

Any idea why?

Thanks Ahmar

4
  • Shouldn't the enclosed by clause have two quote characters? Looks you've only got one Commented May 26, 2014 at 11:03
  • @Clive just tried with two and still the same error Commented May 26, 2014 at 11:04
  • By the way, you can replace ".'"'." with just \" Commented May 26, 2014 at 11:28
  • I did that also still doesn't work Commented May 26, 2014 at 11:28

1 Answer 1

1

This:

OPTIONALLY ENCLOSED BY "

Needs to be

OPTIONALLY ENCLOSED BY '"'

So your code should change to

$sql="LOAD DATA INFILE '$file' 
 INTO TABLE calls 
 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
 LINES TERMINATED BY '\r\n' 

 (`account_code`,`source`,`destination`,`dbcontext`,`caller_id`,`channel`,`destination-channel`,`lastapp`,`lastdata`,`start`,`answer`,`end`,`duration`,`billseconds`,`disposition`,`amaflag`,`call_id`,`userfield`)";

(adding the required single quotes and swapping the order of clauses is all that's changed, but I also changed your concatenation to something a bit more readable).

Sign up to request clarification or add additional context in comments.

3 Comments

Try the edit, you had things slightly the wrong way round (ENCLOSED BY refers to the fields, not the lines). That query no longer gives me a syntax error so you should be good to go
By the way on my local server I am giving the script an absolute path like this C:\wamp\tmp\php790E.tmp but it automatically goes to bin folder and gives me this error c:\wamp\bin\mysql\mysql5.6.12\data\wamp mpphp790E.tmp' not found (Errcode: 22 - Invalid argument) Do you know how to avoid this?
I'm afraid I don't - I use *nix and always use an absolute path (/path/to/file); I haven't run into that problem before, but it seems like your path is being interpreted as a relative one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.