Page 1 of 1
Just trying to count rows for cryin out loud :)
Posted: Fri Jun 13, 2003 11:44 am
by steedvlx
I'm not quite sure how to post code here, but here goes. I'm trying to just count the rows in a table that meet two conditions.
1) must be the logged in user ($session_usernumber)
2) wishlist_indicator must be = 0 (off)
I have tried all day and come up with the query and PhP code. The query did work in MySQL, but the script returns NADA.
Any ideas how I can improve it to....well, actually work for instance.
Here is the code:
Code: Select all
<?php
session_register("cart_count")
if (isset($HTTP_POST_VARS['$session_usernumber'])) {
mysql_select_db($database_kb_conn, $kb_conn);
$query_rs_cart = "SELECT * FROM cart WHERE usernumber = $session_usernumber and wishlist_indicator = 0";
$result = mysql_query($query_rs_cart, $kb_conn) or die(mysql_error());
$row_rs_cart = mysql_fetch_assoc($result);
$cart_count = mysql_num_rows($result);
}
?>
I hope i did that right... As always, thank you for any suggestions.
----------------
SteedVLX
Posted: Fri Jun 13, 2003 11:46 am
by steedvlx
I'm sorry, I guess I need another visit to the posting code part of the orientation.
Posted: Fri Jun 13, 2003 12:35 pm
by twigletmac
steedvlx wrote:I'm sorry, I guess I need another visit to the posting code part of the orientation.
Looks like you just had BBCode disabled so I enabled it.
Mac
Posted: Fri Jun 13, 2003 12:36 pm
by twigletmac
Which version of PHP are you using?
Mac
Posted: Fri Jun 13, 2003 12:50 pm
by steedvlx
Thanks, I'll remember that about the BBCode enabling.
The install shows PHP 4.3.0.0 as my version. It is running in sapi mode if that makes any difference. (Geez, I hope it doesn't)
---------------
SteedVLX
Posted: Fri Jun 13, 2003 1:25 pm
by hedge
Not sure about your original problem but it would be more efficient to do a select count(*) query if you just want the count.
Re: Just trying to count rows for cryin out loud :)
Posted: Fri Jun 13, 2003 1:35 pm
by nielsene
Code: Select all
<?php
session_register("cart_count")
// -------------------------------VVVVV no $ here
if (isset($HTTP_POST_VARS['session_usernumber'])) {
mysql_select_db($database_kb_conn, $kb_conn);
$session_usernumber=$_POST['session_usernumber']; // <---------
$query_rs_cart = "SELECT * FROM cart WHERE usernumber = $session_usernumber and wishlist_indicator = 0";
$result = mysql_query($query_rs_cart, $kb_conn) or die(mysql_error());
$row_rs_cart = mysql_fetch_assoc($result);
$cart_count = mysql_num_rows($result);
}
?>
maybe it's just me, but...
Posted: Fri Jun 13, 2003 1:38 pm
by citizen_cain
I am not sure if this will help but unless I am missing something, if you use the echo command to display your $query_rs_cart string, you will see that $session_usernumber is not going to be the session number but instead it will be the string "$session_usernumber ".
try this:
$query_rs_cart = "SELECT * FROM cart WHERE usernumber =" . $session_usernumber . " and wishlist_indicator = 0";
Posted: Fri Jun 13, 2003 1:43 pm
by nielsene
citzen_cain, PHP will preform variable substitution within double quotes, so the "correct" variable will be inserted in the orginal code. However, the original code never defined the local $session_usernumber so its blank....
Posted: Fri Jun 13, 2003 1:45 pm
by citizen_cain
as you can see I am new to php and you are absolutely right.
thanx
Posted: Fri Jun 13, 2003 2:00 pm
by nielsene
OT, for citizen_cain
Still many people feel doing what you show produces more readable/maintainable code, so its not a bad way to do it. It just wouldn't solve the current problem
Posted: Fri Jun 13, 2003 2:06 pm
by citizen_cain
first of all a dumb question:
OT for me??? What is OT?
Beyond this little juicy nugget of inormation that I have missed in my lifetime, foloowing:
The reason I suggested that was because that is what I had to do in my code for a site I designed for my company (internal site). It is just a simple page that collects information from a form on the previous page and records all that in a mySQL db.
However, since I was using $_POST["whatever"] to retrieve that information, I had to use single quotes when I was putting together the query string.
That was stuck in my head and that's way I made the previous suggestion. Like I said though...new to php and not afraid to admit mistakes even when they are careless
Thanx
Posted: Fri Jun 13, 2003 2:18 pm
by nielsene
OT = off topic
further off-topic:
Its also possible to build a string using braces, such as
Code: Select all
$string = "Hello {$_POST["foo"]}";
Again I think its rather ugly looking and avoid it as much as possible, but it does provide an option....
Posted: Fri Jun 13, 2003 9:14 pm
by steedvlx
Well, let's see;
thanks for the responses everybody, even the OT's
Hedge: I will try the count query just to get it to work, but eventually I want the script to evolve into something that will not just count the rows, but manipulate them them as well. I'm going to try adding a DO WHILE loop later on.
Nielsene, Sorry, I should have mentioned. $session_usernumber was originally session_register'ed during login (sucessful or not). It's available even if it is empty (which would make me cry).
Anything else wrong with the script that would make it not work?
---------------------
SteedVLX
Posted: Fri Jun 13, 2003 11:34 pm
by steedvlx
I think I have found the problem. It seems that I'm missing some very basic rules here. Originally, I put all of my includes within a single set of PhP tags.
You guys are probably laughing, but I thought it made the code cleaner. However, the original script was fine. But, the code block for including that file was nested in an if block. So, I rewrote the code so that the include file could be put outside which is better IMHO since it can always be run and, I can nowclear $userid since it will no longer be needed after initial login.
I read in WROX's beginning PHP4 that you must delimit the code both inside and outside of the actual include file. The resulting (amateurish) code works beautifully with the original script above as includes/common_cart.php.
Code: Select all
<?php
<?php
if (!isset($session_user)) {
session_register("session_user");
session_register("session_usernumber");
session_register("cart_count");
}
if (!$userid) {
$session_user = "Guest";
$cart_count = "No";}
else {
$session_user = $userid;
}
?>
<?php
include('includes/common_cart.php');
?>
----------------
SteedVLX