Jump to content

Archived

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

avo

Count Array

Recommended Posts

HI all

im sure thers is a simple answer for this one been trying to figure it out for a hour now but still can't find my answer

all it is im connecting to a db
grabbing info where the $_SESSION user is pressent to pull in all the id number information a
all i wish to do then is echo out how may id's i have pulled out of the db (in one number a count)

code i can get to work to echo out all the id's but i can not count them

ive been using count ()
my code is :

[code]mysql_connect ($dbhost, $dbuser, $dbpass);
mysql_select_db ($dbname) or die ( mysql_error ());
$query = "SELECT id FROM user_messages WHERE username= '".$_SESSION['user']."'";
$result = mysql_query ($query) or die ( mysql_error () );
while ( $x = mysql_fetch_array($result)) {
echo $x['id'];    
}[/code]

in place of echo $x['id'] ; i would just like to echo out the number of times id has been pulled from the db

thanks in advance

Share this post


Link to post
Share on other sites
You could use mysql_num_rows($query) to know the number of returned rows, or a counter variable ($x = 0; $x++ after each echo).

Share this post


Link to post
Share on other sites
[code]$query = "SELECT COUNT(*) FROM user_messages WHERE username= '{$_SESSION['user']}' ";

$res = mysql_query($query);

$message_count = mysql_result($res, 0);[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=377656:date=May 27 2006, 01:36 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 27 2006, 01:36 PM) [snapback]377656[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]$query = "SELECT COUNT(*) FROM user_messages WHERE username= '{$_SESSION['user']}' ";

$res = mysql_query($query);

$message_count = mysql_result($res, 0);[/code]
[/quote]
Yeah Barand's code is nice but if you want your query to return more than the count you would have to mess with GROUP BY and I don't like the idea of using it :P

Share this post


Link to post
Share on other sites

×

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.