syntax error on select stmt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashraf02
    New Member
    • Feb 2008
    • 53

    syntax error on select stmt

    I am getting this error when i run the php file and cant figure out why. everything seems to look fine. the error i get is:

    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_Title' at line 3
    here is my php code.

    [PHP]<?php
    $conn = mysql_connect ("localhost","r oot","")
    or die (mysql_error ());
    mysql_select_db ("stest",$co nn) or die (mysql_error()) ;

    $display_block = "<h1>Categories </h1>
    <p>select cat to see items.</p>";

    $get_cats = "SELECT Cat_ID, cat_title, cat_description
    FROM store_categorie s
    ORDER BY cat_title";
    $get_cat_res = mysql_query ($get_cats) or die (mysql_error()) ;

    if (mysql_num_rows ($get_cat_res) < 1) {
    $display_block = "<p><em>Sor ry no cats to browse</em></p>";
    } else {

    while ($cats = mysql_fetch_arr ay ($get_cat_res)) {
    $cat_id = $cats [Cat_ID];
    $cat_title = strtoupper (stripslashes ($cats[cat_title]));
    $cat_desc = stripslashes ($cats [Cat_description]);

    $display_block .= "<p><strong >
    <a href=\"$_SERVER[PHP_SELF]?cat_id=$cat_id \">$cat_titl e</a></strong>
    <br>$cat_desc </p>";

    if ($_GET[Cat_ID] == $Cat_ID) {
    $get_items = "SELECT Store_ID, Item_title, Item_Price FROM
    Store_Items WHERE Cat_ID = $Cat_ID
    ORDER BY Item_Title";
    $get_item_resul ts = mysql_query ($get_items) or die (mysql_error()) ;

    if (mysql_num_rows ($get_item_res) < 1 ) {
    $display_block = "<p><em>Sor ry no items in this category</em></p>";
    } else {

    $display_block .= "<ul>";

    while ($items = mysql_fetch_arr ay($get_item_re s)) {
    $item_id = $items[id];
    $item_title = stripslashes($i tems[item_title]);
    $item_price = $items[item_price];

    $display_block .= "<li>
    <a href=\"showitem .php?item_id=$i tem_id\">$item_ title</a>
    </strong> (\$$item_price) ";

    }

    $display_block .= "<ul>";

    }
    }
    }
    }
    ?>
    <html>
    <head>
    <title>My Categories</title>
    </head>
    <body>
    <?php echo $display_block; ?>
    </body>
    </html>[/PHP]

    thanks in advance for ur help.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    When Cat_ID is a character type field, you must enclose the variable within quotes, as in[code=sql]$get_items = "SELECT Store_ID, Item_title, Item_Price FROM
    Store_Items WHERE Cat_ID = '$Cat_ID'
    ORDER BY Item_Title";[/code]This is a MySQL problem, so I'll move this thread.

    Ronald

    Comment

    Working...