Jump to content

Counting in PHP


Andrew R

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.