Jump to content

Counting query


highaspen

Recommended Posts

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);
Link to comment
https://forums.phpfreaks.com/topic/5426-counting-query/
Share on other sites

[!--quoteo(post=357005:date=Mar 21 2006, 09:31 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Mar 21 2006, 09:31 AM) [snapback]357005[/snapback][/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.
Link to comment
https://forums.phpfreaks.com/topic/5426-counting-query/#findComment-19387
Share on other sites

I believe it is something like:

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

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...
Link to comment
https://forums.phpfreaks.com/topic/5426-counting-query/#findComment-19592
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.