Dynamic MySQL query is supposed to order by column name (variable), but won't

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • traineeirishprogrammer
    New Member
    • Jul 2007
    • 24

    Dynamic MySQL query is supposed to order by column name (variable), but won't

    I am currently working on a project where I need to sort my MYSQL query results by different categories. How ever the code does not seem to be working properly and I spend too much time on it already.

    So here is the code:

    [PHP]
    if (isset($_GET['sortby'])){$sort = $_GET['sortby'];}
    else $sort = '';

    $query = "SELECT * FROM list WHERE county = '$counties[$x]' order by '$sort' ";
    $db->query($query );
    [/PHP]

    When I view the results I dont the order I want, the order I posted $_GET

    how ever when I let the query be lets say "bandwidth"

    [PHP] $query = "SELECT * FROM list WHERE county = '$counties[$x]' order by bandwidth ";[/PHP]

    it works.

    can you help me where am I going wrong.
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    order by has to be followed by a column name, your $sort isn't a column name.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Irish.

      Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

      Try using backticks (`) around $sort instead of quotes (').

      Comment

      • traineeirishprogrammer
        New Member
        • Jul 2007
        • 24

        #4
        $sort is actually a variable and could be anything, by any coloumn name I want. But what ever way it reads or parses it, mysql does not seem to be able to get the information from the variable. I tried the backsticks but they dont work I only get an error when I try to fetch the results. using mysql_fetch.

        CAN ANYONE HELP ME IT IS NOT WORKING!!

        thanks

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          The order by clause requires the name of a column that exists in the table. The column name should not be quoted. You can encapsulate the column name in backticks (`), as pbmods suggested.

          Note, that depending on your server and how you created the table, the column names may be case-sensitive.
          (My tests show that columns created using backticks are case-sensitive, whether they are encapsulated in the SELECT query or not)

          If your $sort variable provides a value that fits this description, your query should run fine. If not, it will fail.

          Comment

          • traineeirishprogrammer
            New Member
            • Jul 2007
            • 24

            #6
            Thanks I fixed the problem

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Irish.

              Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

              Comment

              Working...