Question

I keep getting

exception 'PDOException' with message 'SQLSTATE[42000]: 
Syntax error or access violation: 1064 
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 
'(questionid, 'sfname' , 'slname' , 'school' , 'grade' , 'addr' , 'city' , 'state' at line 1' 

from this statement:

$stmt = $db->prepare('SELECT * FROM event_responses WHERE eventid= :eventid ORDER BY userid DESC, field (questionid, \''.implode("' , '", $columns1).'\')');

I echo'ed the statement inside and it looks fine to me:

SELECT *
FROM event_responses
WHERE eventid= :eventid
ORDER BY userid DESC,
      field (questionid, 'sfname' , 'slname' , 'school' , 'grade' , 'addr' , 'city' , 'state' , 'zip' , 'semail' , 'sphone' , 'pfname' , 'plname' , 'pemail' , 'pphone' , 'noattend' , 'regid' , 'submitDate' , 'attended' , 'regmethod')

Why is this happening?

Was it helpful?

Solution

You've hit a strange issue in MySQL:

Note

By default, there must be no whitespace between a function name and the parenthesis following it. This helps the MySQL parser distinguish between function calls and references to tables or columns that happen to have the same name as a function. However, spaces around function arguments are permitted.

Remove the space after field so the expression is:

SELECT *
FROM event_responses
WHERE eventid = :eventid
ORDER BY userid DESC,
         field(questionid, 'sfname' , 'slname' , 'school' , 'grade' , 'addr' , 'city' , 'state' , 'zip' , 'semail' , 'sphone' , 'pfname' , 'plname' , 'pemail' , 'pphone' , 'noattend' , 'regid' , 'submitDate' , 'attended' , 'regmethod')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top