Jump to content


Photo

Count Array


  • Please log in to reply
3 replies to this topic

#1 avo

avo
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • Locationstaffordshire uk

Posted 27 May 2006 - 04:11 PM

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 :

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'];    
}

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

Im loving it ........

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 27 May 2006 - 04:55 PM

You could use mysql_num_rows($query) to know the number of returned rows, or a counter variable ($x = 0; $x++ after each echo).
~ D Kuang

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 27 May 2006 - 08:36 PM

$query = "SELECT COUNT(*) FROM user_messages WHERE username= '{$_SESSION['user']}' ";

$res = mysql_query($query);

$message_count = mysql_result($res, 0);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 27 May 2006 - 11:58 PM

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

$res = mysql_query($query);

$message_count = mysql_result($res, 0);
[/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
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users