0

I am having a problem with syntax errors in the following variable:

$uploadQuery = "
LOAD DATA LOCAL INFILE '".$docRoot."/../../includes/dbUploads/".$fileToUpload."' 
INTO TABLE `promotions` 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'  
";

I know that it has something do do with the escaping of the ' characters in the LOAD DATA... line. But I am stumped when it comes to what exactly the problem is, or how to reword this query in the correct manner.

So, My question is this:

How do i reword the stated variable in the correct manner, as to have no syntax errors relating to it.

If anyone has any suggestions or input with this, it would be greatly appreciated.

Thank You!

1
  • What is your error exactly? Also, did you try to echo $uploadQuery instead of running it from php, and then run it manually and see how can be fixed? Commented Nov 9, 2011 at 20:32

1 Answer 1

3

Missing an escape character, this is the correct string:

$uploadQuery = "
LOAD DATA LOCAL INFILE '".$docRoot."/../../includes/dbUploads/".$fileToUpload."' 
INTO TABLE `promotions` 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '\"' 
ESCAPED BY '\\' 
LINES TERMINATED BY '\\r\\n'  
";

Note the extra \ on the 5th line. It was treating the " as a string terminator. Also another problem (that doesn't cause a syntax error) is on the 7th line, you need to escape the backslashes.

P.S. the markup analyzer even picked it up :P

Edit: you probably also need to change line 7 to ESCAPED BY '\\\\' since this reduces to ESCAPED BY '\\' after PHP parses it.

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

6 Comments

Thanks alot! I guess i did not "know" after all... The root of my problem that :)
By the way, you don't have to concatenate variables to a string if you are using double quotes ". For instance, if you say echo "$foo" it will work just like if you said echo $foo. Just note that in some cases you will need to wrap the variable in { and } so that it can be differentiated from the rest of the string's text.
@imkingdavid if i do not concatenate them, it produces more syntax errors for some reason? But generally i do not.
You have to insert the variable into the string like: "ABC $var".
@BlackberryFan what errors? You should be able to just put, for instance, LOAD DATA INFILE '{$docRoot}/../../includes/dbUploads/{$fileToUpload}' INTO TABLE etc. as long as the string itself is wrapped in " so you don't have to escape the '
|

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.