Jump to content

Archived

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

Andrew R

Counting in PHP

Recommended Posts

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 address

Any help would be much appreciated

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
U can do a query for just the pilots or you can use a php function to get the number rows

query 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.

Ray

Obsidian beat me to it. DAMN my slow fingers LOL

Share this post


Link to post
Share on other sites
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.

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.