Jump to content

Warning: mysql_result() error help please


Samza
Go to solution Solved by Barand,

Recommended Posts

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!

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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?? 

Link to comment
Share on other sites

  • Solution

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) );
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.