Forum Moderators: coopster
I'm trying to learn how to build my own cart. I've scrubbed the web all day and the best tutorial I have found(IMO) is here [devarticles.com].
It's going okay so far(I guess), except I'm stuck on the cart.php file.
I'm getting an error on this line:
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
Could anyone enlighten me on this error? I'm pretty sure it has to do with this portion: '" . GetCartId() . "'
Also, if anyone knows of another good resource I would appreciate a link.
Thanks alot! :)
Birdman
That being said, calling a function within a SELECT statement might be a no no. I've never tried it so I'm not sure. Try setting a var first and using that in the SELECT statement.
And no - nothing's ever easy - especially if you're in a hurry. Isn't there a Murphy's Law about this? :)
$cartid = GetCartId();
$rq = "select count(*) from cart where cookieId='" . $cartid . "' and itemId=" . $itemId;
$result = mysql_query($rq) or die (mysql_errno().": ".mysql_error()."<BR>");
the or die bit is good for checking the error that mysql is sending as opposed to the php error.
helps keep everything seperated logically and can make things easier to work with.
Please post the code surrounding the offending line.
$rq = sprintf(
'select count(*) from cart where cookieId=\'%s\' and itemId=%s',
GetCartId(), $itemId);
$result = mysql_query($rq)
or die (sprintf('%s: %s<br>', mysql_errno(), mysql_error()));
That is the way I would do it. Of course this still does not solve your error.
Andreas
If you have the error number you can then grep errmsg.h and mysqld_error.h for that number. Or you could look up the corresponding error message in docs/mysqld_error.txt. ;)
Other than those purposes I have no idea what it is good for.
It wasn´t my idea to include it into the error message. I just copied that from Adam. I guess you need to ask him.
I know I wrote in my last post that that would be the way I´d do it. I admit it: That was a plain lie, a clear deviation from the truth. ;)
I only wanted to demonstrate the sprintf() usage. I use a wrapper function for mysql_query myself which handles errors:
function my_db_query($sql) {
global $local, $config, $db_prefix, $db_prefix_on_tables;
if (!$local) {
$_db_prefix_on_tables = $db_prefix_on_tables
. 'comments_board¦comments_category¦comments_post¦'
. 'comments_session¦comments_thread¦comments_user¦'
. 'words¦relevancy¦pages¦release';
$sql = preg_replace("/(from¦update¦replace¦into)([\r\n\s]+)($_db_prefix_on_tables)([\r\n\s]*)/smi", "$1$2$db_prefix$3$4", $sql);
}
$query = @mysql_query($sql);
if ($query) {
if (substr(trim($sql), 0, 6) == 'INSERT') {
return mysql_insert_id();
} else {
return $query;
}
} else {
if ($local) {
die('<pre>' . mysql_error() . '<br/>' . $sql . '</pre>');
} else {
mail($config['board_admin'],
$config['board_name'] . " - Fehler",
mysql_error()."\n$sql\n".time(),
"From: $config[board_admin]");
}
}
} As you see there is no mysql_errno() there.
Adam, what´s the purpose of having mysql_errno() in your error message? Do you really look the error message up in mysql´s source code? ;)
Andreas
[edited by: jatar_k at 3:33 am (utc) on Nov. 13, 2002]
[edit reason] stopped side scroll [/edit]
<?phpinclude("db.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}function AddItem($itemId, $qty)
$crtid="'GetCartId()'";$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemID");
$row = mysql_fetch_row($result);
I just printed a good portion of the php manual, so I'm going to see what I can soak in from that. I am a php newbie, but it's great!
[pre]function AddItem($itemId, $qty)
$crtid="'GetCartId()'";[/pre]
should be
[pre]function AddItem($itemId, $qty) [b]{[/b]
$crtid="'GetCartId()'";
# your code
[b]}[/b][/pre]
A function´s code needs to be enclosed in braces.
http://www.php.net/manual/en/functions.php
Andreas
Birdman, re: curly braces - by default I always include them when I'm first writing code. Once I've finished the code then I'll go back in and see if I can remove any - like on single line if statements
if (!isset($somevar))
$another var = $yetanother * $onemore;
If I don't do it this way I'll invariably add another line of code to the 'if' statement and forget to wrap it in the curly braces. Of course the server barks at me and then I go back in and add them.