COUNT(*) confusion

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

COUNT(*) confusion

Post by Steveo31 »

I'm trying to get the total rows in a given table. Then, if that number is bigger than __ display another message.

For learning sakes (I don't really have a travel agency ;)) I made a DB called Vacation. From there it has tables called "jamaica", "bolivia", and "haiti". Those each have 2 rows, "name" and "total". The total is an auto incremental MEDIUMINT.

Here's the part I think I am having problems with:

Code: Select all

$getTotal = "SELECT COUNT(total) FROM jamaica";

$totalResult = mysql_query($getTotal, $db);

if($totalResult > 4){
	echo "The trip is filled.  Please try one of out other options.";
}
How would I get it to see that the count is more than 4 (which it is in my case)?
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Just do it like you would normally so you would use:

Code: Select all

$getTotal = "SELECT COUNT(total) FROM jamaica";
$totalResult = mysql_query($getTotal, $db);
$row = mysql_fetch_row($result);
if($row[0]> 4){
   echo "The trip is filled.  Please try one of out other options.";
}
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ah thanks. Oh, and it's

Code: Select all

$row = mysql_fetch_row($totalResult);
Not

Code: Select all

$row = mysql_fetch_row($result);
:)

I know, it's habit just for "$result"....
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

lol yeah I didn't notice you had $totalResult.
Post Reply