Question

I have the following select query, where I need to add an ORDER BY topic_id DESC. How can I add it?

$sql = "SELECT  
    top_id,
    top_cat,
            COUNT(top_id) AS topic
FROM
    topics
WHERE
    top_cat = ". mysql_real_escape_string($_GET['id']);

I cannot add the ORDER BY right before the last semicolon (;) since it will get an error and, of course, it cannot be placed right before the quotes ("). I also tried to place it before WHERE but again, shows errors later when I introduce if ($row = mysql_fetch_assoc($result)).

Any ideas?

Was it helpful?

Solution

Concat the " order by topic_id desc" before the semi-colon, just like you concat the ID. If that's what you were doing when you say "you can't place it right before the semicolon" I don't know why not.

It's easier to diagnose code errors if you show the code that created the errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top