Eggzorcist Posted January 27, 2011 Share Posted January 27, 2011 Is it possible to use the PDO with a statement that has ORDER BY tablename. The program works fine but when I added the ORDER BY clause it outputted an error. Is it maybe because I set the fetch mode to PDO::FETCH_OBJ? Or is that irrelevant? This is the error that outputs: Fatal error: Uncaught 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 'BY task_position' at line 1' in /tasks.php(38): PDO->query('SELECT * FROM t...') #1 tasks.php(57): tasks->grab_tasks('2') #2 {main} thrown in tasks.php on line 38 It normally worked without the ORDER BY. Here is my MYSQL call: $grabber = $this->pdo->query('SELECT * FROM tasks WHERE user_id = ' . $userid . "ORDER BY 'task_position'"); $grabber->setFetchMode(PDO::FETCH_OBJ); Quote Link to comment https://forums.phpfreaks.com/topic/225888-problems-with-pdo-and-order-by/ Share on other sites More sharing options...
BlueSkyIS Posted January 27, 2011 Share Posted January 27, 2011 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 'BY task_position' at line 1' in /tasks.php(38): echo your SQL and see what it looks like before executing it. Quote Link to comment https://forums.phpfreaks.com/topic/225888-problems-with-pdo-and-order-by/#findComment-1166219 Share on other sites More sharing options...
requinix Posted January 27, 2011 Share Posted January 27, 2011 Try to substitutethe $userid into thequery, mentally, andsee what you come upwith. Quote Link to comment https://forums.phpfreaks.com/topic/225888-problems-with-pdo-and-order-by/#findComment-1166249 Share on other sites More sharing options...
blew Posted January 27, 2011 Share Posted January 27, 2011 man, take attention to your code you're using double in single quotes in diferents ways $grabber = $this->pdo->query('SELECT * FROM tasks WHERE user_id = ' . $userid . "ORDER BY 'task_position'"); do you see? you started with single quote, then, after the . $userid . , you used double quote... or you use double, or you use single Quote Link to comment https://forums.phpfreaks.com/topic/225888-problems-with-pdo-and-order-by/#findComment-1166296 Share on other sites More sharing options...
sasa Posted January 27, 2011 Share Posted January 27, 2011 add some space before ORDER BY " ORDER BY ... Quote Link to comment https://forums.phpfreaks.com/topic/225888-problems-with-pdo-and-order-by/#findComment-1166299 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.