Jump to content


Photo

Counting in PHP


  • Please log in to reply
3 replies to this topic

#1 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 22 May 2006 - 02:35 PM

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?

<?
include 'db.php';

$query_pilots = "SELECT * FROM user_profile");
$pilots = mysql_query($query_pilot) or die(mysql_error());
$row_pilots = mysql_fetch_assoc($pilots);
?>

The field name in the database for the pilots is “UserName”

Other echo's in my table include
<?php echo $row_pilots['Email']; ?>
for the pilots email address

Any help would be much appreciated

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 22 May 2006 - 02:44 PM

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:
<?php
$sql = mysql_query("SELECT COUNT(*) AS count FROM user_profile");
$count = mysql_result($sql, 0, 'count');
echo "$count records returned!";
?>

the other option is to simply use mysql_num_rows() to see how many records were returned:
$sql = mysql_query("SELECT * FROM user_profile");
$count = mysql_num_rows($sql);
echo "$count records returned!";

hope this helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 22 May 2006 - 02:46 PM

U can do a query for just the pilots or you can use a php function to get the number rows

query example:
$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'].'';

PHP example
$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;

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

#4 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 22 May 2006 - 02:56 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users