->count()

->count() -- Perform a select count() request

Synopsis

int $DB_DataObject->count ([string|boolean $countWhat|$whereAddOnly [, boolean $whereAddOnly]])

Description

It performs a select count() request on the tables key column and returns the number of resulting rows. The default condition applied to the count() is a combination of the object variables and whereAdd settings. If the constant DB_DATAOBJECT_WHEREADD_ONLY is passed in as the first parameter then only the whereAdd settings will be used.

Parameter

Return value

int|false - number of results or false on an error

Note

This function can not be called statically.

Example

Example 39-1. Various examples of using count()


<?php

/* using property values */
$person = new DataObjects_Person;
$person->name  "test"
$total $person->count();
echo "There are {$total} people with a name like test";

/* using countWhat */
$person = new DataObjects_Person;
$total $person->count('DISTINCT name');
echo "There are {$total} names in the database";

/* using countWhat value = DISTINCT */
$person = new DataObjects_Person;
$total $person->count('DISTINCT');
echo "There are {$total} names in the database";

/* using whereOnly */
$person = new DataObjects_Person;
$person->name "test";
$person->whereAdd("name like '%test%'");
$total $person->count(DB_DATAOBJECT_WHEREADD_ONLY);
echo "There are {$total} names in the database";

?>

Example 39-2. Resulting SQL

SELECT count(person.id) AS DATAOBJECT_NUM
    FROM person
    WHERE person.name = 'test';

SELECT count(DISTINCT name) AS DATAOBJECT_NUM
    FROM person;

SELECT count(DISTINCT person.id) AS DATAOBJECT_NUM
    FROM person;

SELECT count(person.id) AS DATAOBJECT_NUM
    FROM person
    WHERE name like '%test%';