Michael
Michael

Reputation: 113

Adding ORDER BY after variable in MySQL query

I currently have this line of code, which works perfectly:

$data2 = mysql_query("SELECT * FROM notes WHERE HiveID=" . $HiveID) or die(mysql_error());

I want to reverse the order of my listing, so I tried adding the ORDER BY after WHERE. I tried the following code:

$data2 = mysql_query("SELECT * FROM notes WHERE HiveID=" . $HiveID  . "ORDER BY Date DESC") or die(mysql_error());

This code gave me the error: 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 Date DESC' at line 1.

I am unaware of how I can get this to work, and any help will be greatly appreciated.

Upvotes: 1

Views: 235

Answers (2)

NullPoiиteя
NullPoiиteя

Reputation: 57322

your query is HiveID=" . $HiveID . "ORDER which will be like 5ORDER (if hiveid is 5) so it wont give you result there must be a space before the ORDER try

"SELECT * FROM notes WHERE HiveID=" . $HiveID." ORDER BY Date DESC"

or

"SELECT * FROM notes WHERE HiveID='".$HiveID."' ORDER BY Date DESC"

Upvotes: 1

Ryan
Ryan

Reputation: 144

You are missing a space after the open quotes.

Should be . $hive . " ORDER BY...

Upvotes: 4

Related Questions