pixelJockey
pixelJockey

Reputation: 321

mysql query results sorting

I am calling the results form a query to a list on my site based on if the item is "downtown_hosted". This works fine but I would now like to sort that list DESC, but can't seem to get the syntax correct.

Below is what I have:

$result_events = mysql_query("SELECT * FROM events 
                                WHERE downtown='downtown_hosted'
                                ORDER BY date DESC
                                LIMIT 5 ");

Upvotes: 1

Views: 426

Answers (2)

Hammerite
Hammerite

Reputation: 22340

date is an SQL keyword. You can have a column called date, but every time you refer to it you will have to use identifier quotes. In MySQL this is accomplished using backticks: `date`

Upvotes: 6

Matt
Matt

Reputation: 44058

You need to escape the word "date" with backticks.

E.g.:

$result_events = mysql_query("
    SELECT * FROM events
    WHERE downtown='downtown_hosted'
    ORDER BY `date` DESC
    LIMIT 5
");

In practice it's not a bad habit to get into always enclosing your columns with backticks, so you will not have to worry about conflicting with language keywords.

Upvotes: 6

Related Questions