Functions and MySQL Queries - Is there a step I am missing in between?

Hi,

If I use this code for function GetCartId() will it be actioned (will a cookie be checked for and added if not on computer) with the second bit(s) of coding? I need to know how to use functions in MySQL queries.

Code for function:
<?php
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table

if(isset($_COOKIE[“cookieid”]))
{
return $_COOKIE[“cookieid”];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID

session_start();
setcookie(“cookieid”, session_id(), time() + ((3600 * 24) * 4));
return session_id();
}
}
?>

Will this function be called/used within these querys?:

Query 1:
$result = mysql_query(“select count(*) from cart where cookieid = '” . GetCartId() . “’ and productid = $productid”);

Query 2:
@mysql_query(“insert into cart(cookieid, productid, quantity) values('” . GetCartId() . “', $productid, $quantity)”);

Query 3:
$result = mysql_query(“select * from cart inner join products on cart.productid = products.productid where cart.cookieid = '” . GetCartId() . “’ order by products.name asc”);

Will the queries use the GetCarId() Function or not. Is there a missing step between defining the function and using it within a query?

Matt.

Yes the function will be called.
Did you try it?

I have tried it but no cookie is added to my computer.

Also Query 3 is supposed to get a table to be output in the cart but nothing appears. I am quite sure this is because it says in Query 3 “where cart.cookieid = '” . GetCartId() . “” so if there is no cookie on my computer then nothing will be available in the table. Do you agree that as there is no cookie on my computer this is my problem!?!

The other two queries indeed rely on this function and again, since there is no cookie added to my computer, this is probably the problem.

Any ideas?

Matt.

Probably, yes.
To be sure, change your code into something like this:


$query = "select count(*) from cart where cookieid = '" . GetCartId() . "' and productid = $productid";
echo "query: $query <br />";
$result = mysql_query($query);

Echoing the query you can see what the function returns.
Do the same thing for the other two if needed.

OK - Great!!! I get this output

query: select count(*) from cart where cookieid = ‘91d55fcc57975b03b22c7c0634613b23’ and productid = 3

The good news is this is being added to my MySQL Table as planned.
The bad news is NO COOKIE IS ON MY COMPUTER!!!

I’ve no idea how a cookie can appear in the MySQL table and not appear on my computer. I actually searched for the cookie code on my mac (in the cookie window) and there are ZERO matches.

How can it be stored in my table but not on my computer!?! (This is the first time it has appeared in my table, by the way)

Matt.

I have found the cookie on my computer — I did a search for the web site name and now the cookie appears on my computer — not sure why I had to search for web site name rather than cookie number/code.

So, now the first two queries are working. But the third is not.

<?php>
function ShowCart(){

$result = mysql_query(“select * from cart inner join products on cart.productid = products.productid where cart.cookieid = '” . GetCartId() . “’ order by products.name asc”);
<?>

any ideas why this would not be working? Is there some code I can use in place of it to check the output like you suggested with the first query?

Matt.

Yes. Just like I did with the first query :slight_smile:

Off Topic:

Where are those bbcode tags Matthew? :wink:

Something I am finding very odd is the some code that is not working correctly:


function AddItem($productid, $quantity){

//The main part of the AddItem function checks whether or not this item already exists in the users cart. If it does, then //its quantity field is updated and it isn't added again:

$result = mysql_query("select count(*) from cart where cookieid = '" . GetCartId() . "' and productid = $productid");

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query("insert into cart(cookieid, productid, quantity) values('" . GetCartId() . "', $productid, $quantity)");
}
else
{


// This item already exists in the users cart,
// we will update it instead

UpdateItem($productid, $quantity);


//Looking at the code above, we can see that if $numRows equals zero (i.e. the item isn't already in the users cart) then //the item is added to the cart table. If not, the items quantity field is updated by calling the UpdateItem function, 
//which is described below.

//UpdateItem accepts two parameters, in the same way that the AddItem function does:

function UpdateItem($productid, $quantity){

//It executes a simple UPDATE SQL query against the cart table, updating the quantity of one specific item. The cookieId //field is used to match the users session ID to that particular product, making sure that the quantity is only updated for //that item and the current user:

$query = "update cart set quantity = $quantity where cookieid = '" . GetCartId() . "' and productid = $productid"; 
echo "query: $query <br />"; 
$result = mysql_query($query);

//Removing an item is a simple matter of the RemoveItem function being called. It accepts just one parameter, which is the //ID of the item to delete:

}
}


It keeps executing this bit


@mysql_query("insert into cart(cookieid, productid, quantity) values('" . GetCartId() . "', $productid, $quantity)");

but in the cart table the row does not equal 0 (zero) so it should be using the else statement. Why is it not recognising the fact it needs updating since there is already an entry in the table.

Matt.

P.S. I thought you suggested using [?php] and [?] to ensure it appears correctly - did I remember incorrectly?

The tags are [noparse]

 ... 

[/noparse].

Are you sure?
Do an echo of $numRows to check its value.

Change this line

$result = mysql_query("select count(*) from cart where cookieid = '" . GetCartId() . "' and productid = $productid");

like you did with the other queries, echo $query, and copy and paste it into phpMyAdmin and see what result it gives.

I have just tried echo $result instead as this should count the number of rows but it just outputs:

Resource id #4

This is not a number and even if it were single number cannot be counted to find the number of rows! I think the coding does not make sense. First we ask to count in a query and then we ask to count the number of rows of a number!!

Obviously, if the rows are counted here this will equal 0. So I think the statement is wrong.


function AddItem($productid, $quantity){

//The main part of the AddItem function checks whether or not this item already exists in the users cart. If it does, then //its quantity field is updated and it isn't added again:

$result = mysql_query("select count(*) from cart where cookieid = '" . GetCartId() . "' and productid = $productid");

$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
echo $result;
// This item doesn't exist in the users cart,
// we will add it with an insert query

@mysql_query("insert into cart(cookieid, productid, quantity) values('" . GetCartId() . "', $productid, $quantity)");
}
else
{
  

I have managed to get it partially working with:

$query = mysql_query("SELECT COUNT(*) FROM cart where productid = $productid");
list($number) = mysql_fetch_row($query);
echo "Number of entries=$number";

for some reason it does not work with the code checking the cookieid with the cookie in the computer (which obviously I need for it to work). Would you advise against using list$(number)? And now I know the cookie matching is causing a problem, any ideas why? I thought maybe the dots or quotations (. or ") at the beginning and end of the GetCartId() may be causing a spacing problem. Could this be the case?

Matt.

Matt.

[FPHP]mysql_num_rows[/FPHP]

Unfortunately, mysql_num_rows won’t work because the OP is using count(*).

Maybe something similar to this would be better suited…


<?php
$sql = 'SELECT COUNT(*) FROM table;';
$res = mysql_query($sql);
list($num) = mysql_fetch_array($res, MYSQL_NUM);
echo $num;

OK - will use something like that - I’ll see what fits best.

Regarding the issue I have with the cookieID in my MySQL table not matching the Cookie code on my computer I have tried this:


 <?php
 include("db.php");
if($cookieid == '" . GetCartId() . "'){
	 echo "true";
}
	 else{
		 echo "false";
	 }
	 ?>
     
 
 <?php
if(cookieid == '" . GetCartId() . "'){
	 echo "true";
}
	 else{
		 echo "false";
	 }
	 ?>

and it returns “false false”. It did it with and without “$” as I was not sure if it needed it. Can you suggest if this code is wrong to check the two or confirm that the problem is that the two codes are not the same (for some reason).

Matt.

Sorry yes, I misread the second query thinking it was another SELECT (in which case you’d drop the count and just num_rows the data). It’s an insert however.

Why not just do the insert query, and then test mysql_error for content (If duplicate index, error is populated; if not, query succeeded and error is empty).