Andrew R Posted May 22, 2006 Share Posted May 22, 2006 Hello everyone, this is my first post of hopefully many on this forum.I want to count the amount of pilots in the database and echo them into a field. How do I add this to my query and echo it?[code]<?include 'db.php';$query_pilots = "SELECT * FROM user_profile");$pilots = mysql_query($query_pilot) or die(mysql_error());$row_pilots = mysql_fetch_assoc($pilots);?>[/code]The field name in the database for the pilots is “UserName”Other echo's in my table include [code]<?php echo $row_pilots['Email']; ?>[/code] for the pilots email addressAny help would be much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/10184-counting-in-php/ Share on other sites More sharing options...
obsidian Posted May 22, 2006 Share Posted May 22, 2006 welcome to the forums! i hope you find lots of great help here.there are a couple ways to handle counting records. the first of which is to let SQL do it for you:[code]<?php$sql = mysql_query("SELECT COUNT(*) AS count FROM user_profile");$count = mysql_result($sql, 0, 'count');echo "$count records returned!";?>[/code]the other option is to simply use mysql_num_rows() to see how many records were returned:[code]$sql = mysql_query("SELECT * FROM user_profile");$count = mysql_num_rows($sql);echo "$count records returned!";[/code]hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/10184-counting-in-php/#findComment-37956 Share on other sites More sharing options...
craygo Posted May 22, 2006 Share Posted May 22, 2006 U can do a query for just the pilots or you can use a php function to get the number rowsquery example:[code]$query_pilots = "SELECT COUNT(UserName) AS num_pilots FROM user_profile WHERE fieldname = 'pilots'"; $pilots = mysql_query($query_pilot) or die(mysql_error()); $row_pilots = mysql_fetch_assoc($pilots);echo ''.$row_pilots['num_pilots'].'';[/code]PHP example[code]$query_pilots = "SELECT * FROM user_profile WHERE fieldname = 'pilots'"; $pilots = mysql_query($query_pilot) or die(mysql_error()); $num_pilots = mysql_num_rows($pilots);echo $num_pilots;[/code]Replace fieldname in the above queries with the fieldname where you mark the user as a pilot.RayObsidian beat me to it. DAMN my slow fingers LOL Quote Link to comment https://forums.phpfreaks.com/topic/10184-counting-in-php/#findComment-37957 Share on other sites More sharing options...
Andrew R Posted May 22, 2006 Author Share Posted May 22, 2006 WOW!! you guys are really quick, cheers. When you are totalling up hours is there a certain format you arrange it in i.e do you add SUM or count in the SQL query?Cheers for your help. Quote Link to comment https://forums.phpfreaks.com/topic/10184-counting-in-php/#findComment-37961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.