Zebra_Database::dcount PHP Method

dcount() public method

count male users $male = $db->dcount('id', 'users', 'gender = "M"'); when working with variables you should use the following syntax this way you will stay clear of SQL injections $users = $db->dcount('id', 'users', 'gender = ?', array($gender));
public dcount ( string $column, string $table, string $where = '', array $replacements = '', mixed $cache = false, boolean $highlight = false ) : mixed
$column string Name of the column in which to do the counting. @param string $table Name of the table containing the column. @param string $where (Optional) A MySQL WHERE clause (without the WHERE keyword). Default is "" (an empty string). @param array $replacements (Optional) An array with as many items as the total parameter markers ("?", question marks) in $where. Each item will be automatically {@link escape()}-ed and will replace the corresponding "?". Can also include an array as an item, case in which each value from the array will automatically {@link escape()}-ed and then concatenated with the other elements from the array - useful when using WHERE column IN (?) conditions. See second example {@link query here}. Default is "" (an empty string). @param mixed $cache (Optional) Instructs the library on whether it should cache the query's results or not. Can be either FALSE - meaning no caching - or an integer representing the number of seconds after which the cache will be considered expired and the query executed again. The caching method is specified by the value of the {@link caching_method} property. Default is FALSE. @param boolean $highlight (Optional) If set to TRUE the debugging console will be opened automatically and the query will be shown - really useful for quick and easy debugging. Default is FALSE. @return mixed Returns the number of counted records or FALSE if no records matching the given criteria (if any) were found. It also returns FALSE if there are no records in the table or if there was an error. This method may return boolean FALSE but may also return a non-boolean value which evaluates to FALSE, such as 0. Use the === operator for testing the return value of this method.
$table string
$where string
$replacements array
$cache mixed
$highlight boolean
return mixed
    function dcount($column, $table, $where = '', $replacements = '', $cache = false, $highlight = false)
    {
        // run the query
        $this->query('

            SELECT
                COUNT(' . $column . ') AS counted
            FROM
                `' . $table . '`' . ($where != '' ? ' WHERE ' . $where : ''), $replacements, $cache, false, $highlight);
        // if query was executed successfully and one or more records were returned
        if ($this->last_result !== false && $this->returned_rows > 0) {
            // fetch the result
            $row = $this->fetch_assoc();
            // return the result
            return $row['counted'];
        }
        // if error or no records
        return false;
    }