Jump to content

Warning: mysql_result() error help please


Samza

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
https://forums.phpfreaks.com/topic/277596-warning-mysql_result-error-help-please/
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?

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

 

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.