1

I am using multiple queries and it's working fine, the only issue I am facing is that order by is not working. I tried some code like

$query = "SELECT  id, name,  reg_number, class, section  FROM register  where id IS NOT NULL ORDER BY `id` DESC";

it's working fine in phpmyadmin and giving me a proper result. But it's not working where I want to use it.

$query = "SELECT  id, name,  reg_number, class, section  FROM register  where id IS NOT NULL ";

    if ( $name !="" ){
        $query .= " AND `name` LIKE '".$name."%'"; // id is greater then 
     }  

    if ( $status !="" ){
        $query .= " AND `status` LIKE '".$status."%'"; // id is greater then 
     }

     if ( $id_from !="" ){
        $query .= " AND id >= $id_from "; // id is greater then 
     }

     if ( $id_to !="" ){

        $query .= " AND id <= $id_to ";  // id is shorter then 
     } 

      if ( $class !="" ){
        $query .= " AND class IN($class)"; // Selecting class 
     }

     if ( $section !="" ){
        $query .= " AND section IN($section)"; // selecting section
     }

$result = mysql_query($query);

I want to use order by in this query but order by is not working with this.

 $query = "SELECT  id, name,  reg_number, class, section  FROM register  where id IS NOT NULL ORDER BY `id` DESC";

AND also used

 $query = "SELECT  id, name,  reg_number, class, section  FROM register  where id IS NOT NULL ORDER BY id DESC";

I don't know what's the problem with my code.

Alvaro Menéndez
  • 8,766
  • 3
  • 37
  • 57
jitu
  • 21
  • 3
  • `var_dump($query)` before the `$result` line and what do you get? From the code, it looks like it will end up being invalid SQL if you're just swapping out the first line. – Jonnix May 07 '15 at 14:09
  • 1
    Are you appending additional WHERE conditions after the ORDER BY? The ORDER BY has to be at the end... – jarlh May 07 '15 at 14:10
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 07 '15 at 14:13
  • Thanks for your comments. I am learning prepared statements. I have started on that.. – jitu May 07 '15 at 17:09

1 Answers1

3

Just add ORDER BY when finish your WHERE:

     if ( $section !="" ){
        $query .= " AND section IN($section)"; // selecting section
     }

$query .= " ORDER BY id DESC";
$result = mysql_query($query);
Alex
  • 16,739
  • 1
  • 28
  • 51
  • Thanks Alex it works fine, i was fighting with this code from last 6 hours , i tried the same $query .= " ORDER BY id DESC"; but i was trying it before if condition, that's why it was not displaying any result.. Thanks a lot man.... – jitu May 07 '15 at 16:59