Jump to content


Photo

Counting query


  • Please log in to reply
3 replies to this topic

#1 highaspen

highaspen
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 21 March 2006 - 04:07 PM

I think I just need some help thinking through this. I'm using the query below but it's returning 0 and I knwo that to be innacurate.

$sql='SELECT COUNT(*) FROM members WHERE owner="$valid_user"';

Here is what I want it to do: count the number of times the $valid_user appears in the owner column in the members table. I'm wondering if I have a logic problem. I'm not getting any mysql errors during the query but when calling the aray in to the page using:

<? echo ($rs['0']); ?>

I'm getting zero. Below is the full code for my query (FYI). Thanks to all you smart people helping this dumb guy.

--------------------------------

include ("../global_vars.php");

$valid_user = $_POST['userid'];

$conn=@mysql_connect("$dbserver", "$dbuser","$dbpass")
or die ('Could not connect. Error: ' . mysql_error());

$rs = @mysql_select_db("$dbtable", $conn)
or die ('Could not select database. Error: '. mysql_error());

$sql='SELECT COUNT(*) FROM members WHERE owner="$valid_user"';

#execute the query
$rs=mysql_query($sql,$conn)
or die('Could not execute query. Error: '. mysql_error());

$rs = mysql_fetch_array($rs);

#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 21 March 2006 - 04:31 PM

$sql='SELECT owner FROM members WHERE owner="$valid_user"';
$rs=mysql_query($sql,$conn);

$rows_returned = mysql_num_rows($rs);

echo $rows_returned;
"Confidence in the face of risk."

#3 highaspen

highaspen
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 21 March 2006 - 04:50 PM

[!--quoteo(post=357005:date=Mar 21 2006, 09:31 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 21 2006, 09:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$sql='SELECT owner FROM members WHERE owner="$valid_user"';
$rs=mysql_query($sql,$conn);

$rows_returned = mysql_num_rows($rs);

echo $rows_returned;
[/quote]


Thanks man... lookis like it's working.

Cheers.

#4 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 22 March 2006 - 09:28 AM

I believe it is something like:

<?php echo mysql_query("SELECT count(*) as num FROM members WHERE owner='".$valid_user."'", $conn); ?>

I would do it micah's way though so I could requisition the information from the database as well as the count. With this method you must do a query to get the count then query again to get what information you might wish to display or use. I only post this because it is the way you were attempting at first. I forget now if this is correct or not and I'm half asleep, but if this isn't exactly right it is pretty close if memory serves...

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users