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
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.

Link to comment
Share on other sites

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

;)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.