Just trying to count rows for cryin out loud :)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Just trying to count rows for cryin out loud :)

Post 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
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

I'm sorry, I guess I need another visit to the posting code part of the orientation.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which version of PHP are you using?

Mac
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post 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
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: Just trying to count rows for cryin out loud :)

Post 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);
}
?>
citizen_cain
Forum Newbie
Posts: 7
Joined: Fri Jun 13, 2003 1:38 pm

maybe it's just me, but...

Post 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";
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
citizen_cain
Forum Newbie
Posts: 7
Joined: Fri Jun 13, 2003 1:38 pm

Post by citizen_cain »

as you can see I am new to php and you are absolutely right.

thanx
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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
Last edited by nielsene on Fri Jun 13, 2003 2:16 pm, edited 1 time in total.
citizen_cain
Forum Newbie
Posts: 7
Joined: Fri Jun 13, 2003 1:38 pm

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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....
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post 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
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post 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. :D

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
Post Reply