veo Posted April 11, 2008 Share Posted April 11, 2008 I'm doing a tutorial on a storefront then shopping cart and I keep getting this error: 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 1 I'm using php 5.2.4 with mysql 3.22 I just can't seem to figure out why. Here's the code so far. Any help would be greatly appreciated. <?php //connect to database $conn = mysql_connect("sql.com", "user", "pass") or die(mysql_error()); mysql_select_db("hybridmu",$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> > $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 = $cat_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 = $cat_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>"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Store</title> </head> <body> <?php echo $display_block; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/100576-you-have-an-error-in-your-sql-syntax/ Share on other sites More sharing options...
uniflare Posted April 11, 2008 Share Posted April 11, 2008 make sure you enclose values in single quotes in queries, eg: $get_colors = "select item_color from store_item_color where item_id = '$cat_id' order by item_color"; its also a good idea to enclose everything like fields and tablenames in backticks: $get_colors = "select `item_color` from `store_item_color` where `item_id` = '$cat_id' order by `item_color`"; hope this helps, Link to comment https://forums.phpfreaks.com/topic/100576-you-have-an-error-in-your-sql-syntax/#findComment-514396 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.