Order By

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poreko
    New Member
    • Nov 2006
    • 38

    Order By

    Hi

    I am trying to order the following query by price when the varaible $concern=="pric e"; But it is not working. Here is my code.

    Code:
     
     
        for($key=0 ;$key<count($first);$key++)
      {
       if(($first[$key])>0.1){
                $blank++;
                $set=$key+1;
            if($concern=="price"){
                $sql2 = "SELECT * FROM laptop2 WHERE CODE=$set ORDER BY PRICE";
                $query2 = $DB->Query($sql2);
               }
     
     echo'<p> <hr noshade="noshade" /></p>';
       }
     
                   if($array = $DB->FetchArray($query2)){
     
        echo "<table  bgcolor=LightGrey border=1>\n";
        echo"<tr><td>ID</td><td>Brand</td><td>Model</td><td>Processor (GHz)</td>
        <td>Hard Drive (GB)</td><td> Graphic card (MB)</td>
        <td>Battery life(Hours)</td><td>Multimedia</td><td>Price (£)</td></tr>\n";
      // do {
     
     printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</d><td>%s</d><td>%s</d><td>%s</d> </tr>",$array['CODE'],$array['BRAND'],$array['MODEL'],$array['PROCESSOR (Ghz)'],
                $array["STORAGE (GB)"],$array["GRAPHIC CARD (MB)"],$array["BATTERY"],$array["MULTIMEDIA"],$array["PRICE"]);
         // print_r($array);
     
        }while($array=$DB->FetchArray($query2));
       echo"</table>\n";
    Can you spot something?

    Thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I doubt your column 'price' is uppercase (PRICE - like you wrote in your query). Make it lowercase. Also, for the future's sake, you should *really* be using back-ticks around your column names and table names.

    Code:
    SELECT
        *
    FROM
        `laptop2`
    WHERE
        `code` = '{$set}'
    ORDER BY
        `price`
    However, your columns may be uppercase and, if so, disregard my suggestion.

    Comment

    • poreko
      New Member
      • Nov 2006
      • 38

      #3
      Thank you for your suggestions but in my columns price is uppercase.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Does your $DB->query() return any errors? Try echo()ing mysql_error() after you have performed the query.

        Comment

        • poreko
          New Member
          • Nov 2006
          • 38

          #5
          $DB->query() does not return errors. What do you mean by echo()ing mysql_error()?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            You can get the last error generated by mysql by mysql_error(). So, after the query, do this:

            Code:
            die(mysql_error());

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              That would be your standard:
              [code=mysql]$result = mysql_query($sq l) or die(mysql_error ());[/code]

              If you have created yourself a class to handle the database interactions, you should add something to capture the error in case the query fails.

              Edit: Seconds to late :]

              Comment

              • poreko
                New Member
                • Nov 2006
                • 38

                #8
                I already have that added it my class

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  OK. What exactly is not working?

                  Comment

                  • poreko
                    New Member
                    • Nov 2006
                    • 38

                    #10
                    It is not sorting it by price, the results are still random.

                    Comment

                    Working...