PHP MySQL error

Discussion in 'PHP' started by coljo, Sep 22, 2008.

  1. #1
    I have an error in mysql syntax:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by item_color' at line 2

    Here is the code:

    <?php
       //connect to database
       $conn = mysql_connect("localhost", "root", "root")
           or die(mysql_error());
       mysql_select_db("testDB",$conn) or die(mysql_error());
       
       $display_block = "<h1>My Store - Item Detail</h1>";
       
       //validate item
      $get_item = "select c.cat_title, si.item_title, si.item_price,
      si.item_desc, si.item_image from store_items as si left join
      store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";
      
      $get_item_res = mysql_query($get_item) or die (mysql_error());
      
      if (mysql_num_rows($get_item_res) < 1) {
         //invalid item
         $display_block .= "<P><em>Invalid item selection.</em></p>";
      } else {
         //valid item, get info
         $cat_title = strtoupper(stripslashes(
              mysql_result($get_item_res,0,'cat_title')));
         $item_title = stripslashes(mysql_result($get_item_res,0,'item_title'));
         $item_price = mysql_result($get_item_res,0,'item_price');
         $item_desc = stripslashes(mysql_result($get_item_res,0,'item_desc'));
         $item_image = mysql_result($get_item_res,0,'item_image');
      
         //make breadcrumb trail
         $display_block .= "<P><strong><em>You are viewing:</em><br>
         <a href=\"seestore.php?cat_id=$cat_id\">$cat_title</a>
          &gt; $item_title</strong></p>
      
         <table cellpadding=3 cellspacing=3>
         <tr>
         <td valign=middle align=center><img src=\"$item_image\"></td>
         <td valign=middle><P><strong>Description:</strong><br>$item_desc</p>
         <P><strong>Price:</strong> \$$item_price</p>";
      
         //get colors
         $get_colors = "select item_color from store_item_color where
          item_id = $item_id order by item_color";
         $get_colors_res = mysql_query($get_colors) or die(mysql_error());
      
         if (mysql_num_rows($get_colors_res) > 0) {
      
             $display_block .= "<P><strong>Available Colors:</strong><br>";
      
             while ($colors = mysql_fetch_array($get_colors_res)) {
                 $item_color = $colors['item_color'];
      
                 $display_block .= "$item_color<br>";
             }
         }
      
         //get sizes
         $get_sizes = "select item_size from store_item_size where
              item_id = $item_id order by item_size";
         $get_sizes_res = mysql_query($get_sizes) or die(mysql_error());
      
         if (mysql_num_rows($get_sizes_res) > 0) {
     
             $display_block .= "<P><strong>Available Sizes:</strong><br>";
      
             while ($sizes = mysql_fetch_array($get_sizes_res)) {
                 $item_size = $sizes['item_size'];
      
                 $display_block .= "$item_size<br>";
             }
         }
      
         $display_block .= "
         </td>
         </tr>
         </table>";
      
       }
      ?>
      <HTML>
      <HEAD>
      <TITLE>My Store</TITLE>
      </HEAD>
      <BODY>
      <? print $display_block; ?>
      </BODY>
      </HTML>
    
    Code (markup):


    I dont know where is my error.

    Please Help!!!!
     
    coljo, Sep 22, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Try changing the following line from:
         $get_colors = "select item_color from store_item_color where
          item_id = $item_id order by item_color";
    
    PHP:
    To:
         $get_colors = "SELECT `item_color` FROM `store_item_color` WHERE `item_id` = '$item_id' ORDER BY `item_color`";
    
    PHP:
    Jay
     
    jayshah, Sep 22, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    if your id is not numeric make sure you include a quote as Jay said:

    WHERE `item_id` = '$item_id'
    PHP:
    And for me I will include ASC or DESC when i use ORDER BY
    ORDER BY `item_color` ASC"
    PHP:
    i wonder if that matters

    good luck
     
    ads2help, Sep 22, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's ASC by default, so not necessary
     
    JAY6390, Sep 22, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    oh =x Thanks :D
     
    ads2help, Sep 22, 2008 IP