Forums

PHP

This topic is locked

Error in SQL Syntax

Posted 26 Feb 2008 10:58:24
1
has voted
26 Feb 2008 10:58:24 Nkosinathi Khoza posted:
Can anyone please tell me where is the error in this MySQL Syntax

$sql=
" LOAD DATA LOCAL INFILE '" .
@mysql_escape_string($this->file_name) .
"' INTO TABLE DistributionGroup(MobileNumber, FirstName)FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' ";

Also if i remove the code from FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' code it works but i need to do in order to record the data correctly

Below is the error message i obtain
<b>
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 'FIELDS TERMINATED BY ',' LINES TERMINATED BY ' '' at line 1</b>

Replies

Replied 26 Feb 2008 11:19:55
26 Feb 2008 11:19:55 Alan C replied:
it's really hard to look for a syntax error in that line because of all the function and object code, try putting an echo statement into your code immediately after the assignment of $sql, it would look something like . . .

<pre id=code><font face=courier size=2 id=code>echo __LINE__.' $sql is '.$sql.' &lt;br /&gt;'; </font id=code></pre id=code>

then you can see exactly what the query looks like at that point, then, copy it and paste it into the sql execution section of phpmyadmin and execute it, you'll probably see something really obvious
Replied 26 Feb 2008 11:38:19
26 Feb 2008 11:38:19 Nkosinathi Khoza replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
it's really hard to look for a syntax error in that line because of all the function and object code, try putting an echo statement into your code immediately after the assignment of $sql, it would look something like . . .

<pre id=code><font face=courier size=2 id=code>echo __LINE__.' $sql is '.$sql.' &lt;br /&gt;'; </font id=code></pre id=code>

then you can see exactly what the query looks like at that point, then, copy it and paste it into the sql execution section of phpmyadmin and execute it, you'll probably see something really obvious
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

When i use the echo_LINE Command as advised below is what I get.
LOAD DATA LOCAL INFILE 'C:\\WINNT\\Temp\\php6778.tmp' INTO TABLE DistributionGroup(MobileNumber, FirstName)FIELDS TERMINATED BY ',' LINES TERMINATED BY ' '

Unable to run 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 'FIELDS TERMINATED BY ',' LINES TERMINATED BY ' '' at line 1
Replied 27 Feb 2008 11:17:24
27 Feb 2008 11:17:24 Alan C replied:
HI
great to see the line works and does print something, as it works without the last part of the query that is probably where the problem lies.

Looking at the documentation for load data local infile at dev.mysql.com/doc/refman/6.0/en/load-data.html it looks like there are some special cases where the line terminator is an empty string, like ' '

that might be the case because your line ends FirstName)FIELDS TERMINATED BY ',' LINES TERMINATED BY ' '

maybe it needs a space between the ) and FIELDS although I am not sure if whitespace is important there

the more likely thing is that LINES TERMINATED BY ' ' which looks like a space or empty string

What could be happening is that because there are double quotes " around the string, the \n is being evaluated, that can happen when i'm trying to send newlines to html, it may be worth re-arranging the code to try to avoid that.

perhaps

<pre id=code><font face=courier size=2 id=code> $sql=
" LOAD DATA LOCAL INFILE '" .
@mysql_escape_string($this-&gt;file_name) .
"' INTO TABLE DistributionGroup(MobileNumber, FirstName)FIELDS TERMINATED BY ','
LINES TERMINATED BY ".'\'\n\'' ;</font id=code></pre id=code>

by escaping the single quotes like that it may force the newline character to be sent

Reply to this topic