Jump to content

[SOLVED] PHP/MySQL Results Invalid


techtheatre

Recommended Posts

I know this question probably has more to do with SQL than PHP, except i have checked the SQL and it seems fine.  I have a basic shopping cart script i am building and the items in the cart are stored in a table in my DB.  I have the following function that should display a "quick summary" of the number of items in the cart.  Earlier on the page i have included { really i used a require_once() } the $db connection info.

 

function showCartSummary() {
$NumItemsInCart = "0";  //reset to zero for default
$sql = "SELECT SUM(Quantity) FROM ".DB_TABLE_PREFIX."CartItems WHERE CartId='$cartid' ";
$result = @mysql_query($sql, $db);
if($result)
{
	$row = mysql_fetch_array($result);
	$NumItemsInCart = $row[0];
}

if ($NumItemsInCart == "0") {
	return "$sql<br />$NumItemsInCart<p><font size=\"-1\">You have no items in your shopping cart</font></p>";
} else {
	$s = ($NumItemsInCart > 1) ? 's':'';  //make item(s) plural if more than one
	return "$sql<br />$NumItemsInCart<p><font size=\"-1\"><a href=\"cart.php\">You have $NumItemsInCart item".$s." <br />in your cart</a></font><br /><a href=\"cart.php\">VIEW CART</a></p>";
}
}

I am having the query returned at this point for troubleshooting, and get the correct query returned as follows:

SELECT SUM(Quantity) FROM demo_CartItems WHERE CartId='123'

 

When i copy/paste that query into phpMyAdmin it properly returns a value of "9"...but when i run the function it always says i have no items in my cart (first option).

 

Here is the data in my table (demo_CartItems):

 

Item CartId ItemName ItemId Quantity

3 123 TST 251 4

2 123 Product2 250 1

8 123 CPY 252 2

7 123 CPY 252 1

9 123 CPY 252 1

 

 

Any help is greatly appreciated.  I have been hitting my head on this one for a while now...i am sure i have stupidly overlooked something obvious.  Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/47501-solved-phpmysql-results-invalid/
Share on other sites

function showCartSummary() {
        global $db;
$NumItemsInCart = "0";  //reset to zero for default
$sql = "SELECT SUM(Quantity) AS qty FROM ".DB_TABLE_PREFIX."CartItems WHERE CartId='$cartid' ";
$result = @mysql_query($sql, $db);
if($result)
{
	$row = mysql_fetch_array($result);
	$NumItemsInCart = $row['qty'];
}

if (!$NumItemsInCart) {
	return "$sql<br />$NumItemsInCart<p><font size=\"-1\">You have no items in your shopping cart</font></p>";
} else {
	$s = ($NumItemsInCart > 1) ? 's':'';  //make item(s) plural if more than one
	return "$sql<br />$NumItemsInCart<p><font size=\"-1\"><a href=\"cart.php\">You have $NumItemsInCart item".$s." <br />in your cart</a></font><br /><a href=\"cart.php\">VIEW CART</a></p>";
}
}

 

The "AS" syntax should be what you need here.  Look how I modified the query and the result returned so you can see for future reference.

i had already tried it with an AS line in there (which i do not commonly use).  Unfortunately that still gave the same result.

 

You DID however solve the problem...but i think it was the inclusion of the following:

 

        global $db;

 

I will have to read a bit more to find out exactly what that does, but it seems to have solved the problem! 

THANKS!

PHP, as almost every other programming language (I believe), has variable scopes.  This means that variables defined outside of a function and a class are on a different scope than those defined in a class, and again in a different scope from those defined in a function.  If you want to access a variable that was declared outside of a function, meaning it was defined globally, you must precede the variable with global, which tells PHP to pull that variable from the global scope, not the internal function scope.

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.