avo Posted May 27, 2006 Share Posted May 27, 2006 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 aall 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 dbthanks in advance Link to comment https://forums.phpfreaks.com/topic/10588-count-array/ Share on other sites More sharing options...
poirot Posted May 27, 2006 Share Posted May 27, 2006 You could use mysql_num_rows($query) to know the number of returned rows, or a counter variable ($x = 0; $x++ after each echo). Link to comment https://forums.phpfreaks.com/topic/10588-count-array/#findComment-39498 Share on other sites More sharing options...
Barand Posted May 27, 2006 Share Posted May 27, 2006 [code]$query = "SELECT COUNT(*) FROM user_messages WHERE username= '{$_SESSION['user']}' ";$res = mysql_query($query);$message_count = mysql_result($res, 0);[/code] Link to comment https://forums.phpfreaks.com/topic/10588-count-array/#findComment-39536 Share on other sites More sharing options...
poirot Posted May 27, 2006 Share Posted May 27, 2006 [!--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 Link to comment https://forums.phpfreaks.com/topic/10588-count-array/#findComment-39568 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.