Jump to content

Problems with PDO and ORDER BY


Eggzorcist

Recommended Posts

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);

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/225888-problems-with-pdo-and-order-by/
Share on other sites

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.

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

;)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.