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

  • string $countWhat - by default count will count on the primary key, if you need to count something else, if you just say DISTINCT, it will count the primiary key prefixed with distinct, or put your own value in (don't forget to escape it if necessary)

  • boolean $useWhereAddOnly - use only the whereAdd conditions (by default, count will only use both the object settings and the whereAdd conditions)

Return value

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

Note

This function can not be called statically.

Example

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";

?>

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%';
fetch next row (Previous) Insert current objects variables into database (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.