Samza Posted May 3, 2013 Share Posted May 3, 2013 Hi, I am having a problem with fixing this mysql error; Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/magics/public_html/newagedclothing/content/checkout.php on line 8 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 'as items FROM shopping_cart WHERE cart_identifier=b221125f5badb81dd17ea21e384533' at line 1 This is the line 8 it is refering to; $num_items = mysql_result(mysql_query("SELECT COUNT * as items FROM shopping_cart WHERE cart_identifier=". mysql_escape_string($cart_id) .""),0); and this is what it looks like when I echo it out; mysql_result(mysql_query("SELECT COUNT * as items FROM shopping_cart WHERE cart_identifier=b221125f5badb81dd17ea21e38453328"),0) I can seem to see the problem but hopefuly someone here can see through this. Thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 Two errors in syntax COUNT is a function so should be COUNT(*) . The $cart_id is a string value so should be in single quotes ... WHERE cart_identifier='b221125f5badb81dd17ea21e38453328' Quote Link to comment Share on other sites More sharing options...
Yohanne Posted May 3, 2013 Share Posted May 3, 2013 try this $num_items = mysql_result(mysql_query("SELECT COUNT * as items FROM shopping_cart WHERE cart_identifier = . mysql_escape_string($cart_id) .''"),0); Quote Link to comment Share on other sites More sharing options...
ecce Posted May 3, 2013 Share Posted May 3, 2013 You need to surround cart_identifier with single quotes ('). It's a CHAR i guess? $num_items = mysql_result(mysql_query("SELECT COUNT * as items FROM shopping_cart WHERE cart_identifier = '".mysql_escape_string($cart_id) .'"),0); Quote Link to comment Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 Read the posts - I've just pointed out that it should be COUNT(*) Quote Link to comment Share on other sites More sharing options...
ecce Posted May 3, 2013 Share Posted May 3, 2013 Read the posts - I've just pointed out that it should be COUNT(*) ...and that the char_identifier needs to be quoted. Sorry... my bad. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 try this $num_items = mysql_result(mysql_query("SELECT COUNT * as items FROM shopping_cart WHERE cart_identifier = . mysql_escape_string($cart_id) .''"),0); jason_ph, please explain the point of posting a solution(?) with exactly the same errors that were already in the original and also adding a couple of extra errors? Quote Link to comment Share on other sites More sharing options...
Samza Posted May 3, 2013 Author Share Posted May 3, 2013 Thanks for your help guys, I tried count (*) but it was the space between count and (*) that was the main problem! sorted now However this is an error im having on this line mysql_result(mysql_query("SELECT SUM(product_qty * product_price) AS subtotal WHERE cart_identifier='". mysql_escape_string($cart_id) ."'"), 0); error message Warning: mysql_result() expects parameter 1 to be resource, boolean given in /checkout.php on line 64 Quote Link to comment Share on other sites More sharing options...
Barand Posted May 3, 2013 Share Posted May 3, 2013 Don't nest mysql functions. It looks as though there is an error but you cannot check for them because of the nesting. $sql = "SELECT SUM(product_qty * product_price) AS subtotal WHERE cart_identifier='". mysql_escape_string($cart_id) ."'"; $result = mysql_query($sql); if (!$result) die (mysql_error() . "<pre>$sql</pre>"); $subtotal = mysql_result($result, 0); Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted May 3, 2013 Share Posted May 3, 2013 You can refer to this. It is telling you that your first parameter in incorrect, so your mysql_query. The page I linked to also states that this is the slowest out of all of the functions you could use to fetch information from a database. Something I am not 100% sure about, but I would not practice, maybe one of the gurus can explain it, but I do not think it is best practices to put concatenation into a query. Turn your mysql_real_escape_string(cart_id) into a variable. I am not sure if that is a smart decision, but that is what I would do and have done in the past. Also, something I just noticed. Can you mysql_real_escape_string and id number. If it is an integer why are you trying to escape it?? Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 3, 2013 Solution Share Posted May 3, 2013 I confess I am not a great fan of concatenation. It takes me back to my VB days when it was the only way and resulted in horrendous multiple quote marks. I prefer to use this so the syntax and quotes are more easily readable $id = mysql_real_escape_string($cart_id) ; $sql = "SELECT SUM(product_qty * product_price) AS subtotal WHERE cart_identifier = '$id' "; or, if the id is numeric and not a string $id = intval($cart_id) ; $sql = "SELECT SUM(product_qty * product_price) AS subtotal WHERE cart_identifier = $id "; or $sql = sprintf ("SELECT SUM(product_qty * product_price) AS subtotal WHERE cart_identifier = '%s' ", mysql_real_escape_string($cat_id) ); Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted May 4, 2013 Share Posted May 4, 2013 ^^^^^^^That's the guy to listen to! Thanks for answering my question within a question. Quote Link to comment Share on other sites More sharing options...
Samza Posted May 4, 2013 Author Share Posted May 4, 2013 Thanks very much for your help Barand. I will never nest mysql querys again! Muchas gracis! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.