sherly
sherly

Reputation: 305

Unable to use ORDER BY in a simple query

I really wonder why data doesn't show whenever I use ORDER BY in such a simple query below:

I've already created a column named 'order' in categories table. The order columns holds the seniority level each category as in the image:enter image description here

        <?php
         include('inc/config.php');
         $sql= "SELECT * FROM categories ORDER BY order DESC";
         $stmt = $pdo->query($sql); 
        ?>
         <h1 class="text-center">Choose a <span class="elec">level</span></h1>
        <?php
        while($row = $stmt->fetch(PDO::FETCH_ASSOC))
        {
        ?>
        <input type="checkbox" class="level" id=<?php echo $row['catid'];?> value="<?php echo $row['catid'];?>">
        <label><?php echo $row['catname'];?></label><br/>

        <?php
        }
        ?>

Upvotes: 0

Views: 31

Answers (2)

Joel Hinz
Joel Hinz

Reputation: 25404

The word order is a reserved word in MySQL - because it's used in ORDER BY clauses. If you surround it by backticks, it will work:

SELECT * FROM categories ORDER BY `order` DESC

Upvotes: 1

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

order is a reserved word and you need to escape using backticks

SELECT * FROM categories ORDER BY `order` DESC

http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html

Upvotes: 1

Related Questions