PMA\libraries\Table::countRecords PHP Method

countRecords() public method

Counts and returns (or displays) the number of records in a table
public countRecords ( boolean $force_exact = false ) : mixed
$force_exact boolean whether to force an exact count
return mixed the number of records if "retain" param is true, otherwise true
    public function countRecords($force_exact = false)
    {
        $is_view = $this->isView();
        $db = $this->_db_name;
        $table = $this->_name;
        if ($this->_dbi->getCachedTableContent(array($db, $table, 'ExactRows')) != null) {
            $row_count = $this->_dbi->getCachedTableContent(array($db, $table, 'ExactRows'));
            return $row_count;
        }
        $row_count = false;
        if (!$force_exact) {
            if ($this->_dbi->getCachedTableContent(array($db, $table, 'Rows')) == null && !$is_view) {
                $tmp_tables = $this->_dbi->getTablesFull($db, $table);
                if (isset($tmp_tables[$table])) {
                    $this->_dbi->cacheTableContent(array($db, $table), $tmp_tables[$table]);
                }
            }
            if ($this->_dbi->getCachedTableContent(array($db, $table, 'Rows')) != null) {
                $row_count = $this->_dbi->getCachedTableContent(array($db, $table, 'Rows'));
            } else {
                $row_count = false;
            }
        }
        // for a VIEW, $row_count is always false at this point
        if (false !== $row_count && $row_count >= $GLOBALS['cfg']['MaxExactCount']) {
            return $row_count;
        }
        if (!$is_view) {
            $row_count = $this->_dbi->fetchValue('SELECT COUNT(*) FROM ' . Util::backquote($db) . '.' . Util::backquote($table));
        } else {
            // For complex views, even trying to get a partial record
            // count could bring down a server, so we offer an
            // alternative: setting MaxExactCountViews to 0 will bypass
            // completely the record counting for views
            if ($GLOBALS['cfg']['MaxExactCountViews'] == 0) {
                $row_count = false;
            } else {
                // Counting all rows of a VIEW could be too long,
                // so use a LIMIT clause.
                // Use try_query because it can fail (when a VIEW is
                // based on a table that no longer exists)
                $result = $this->_dbi->tryQuery('SELECT 1 FROM ' . Util::backquote($db) . '.' . Util::backquote($table) . ' LIMIT ' . $GLOBALS['cfg']['MaxExactCountViews'], null, DatabaseInterface::QUERY_STORE);
                if (!$this->_dbi->getError()) {
                    $row_count = $this->_dbi->numRows($result);
                    $this->_dbi->freeResult($result);
                }
            }
        }
        if ($row_count) {
            $this->_dbi->cacheTableContent(array($db, $table, 'ExactRows'), $row_count);
        }
        return $row_count;
    }

Usage Example

Example #1
0
/**
 * Function to calculate new pos if pos is higher than number of rows
 * of displayed table
 *
 * @param String   $db    Database name
 * @param String   $table Table name
 * @param Int|null $pos   Initial position
 *
 * @return Int Number of pos to display last page
 */
function PMA_calculatePosForLastPage($db, $table, $pos)
{
    if (null === $pos) {
        $pos = $_SESSION['tmpval']['pos'];
    }
    $_table = new Table($table, $db);
    $unlim_num_rows = $_table->countRecords(true);
    //If position is higher than number of rows
    if ($unlim_num_rows <= $pos && 0 != $pos) {
        $pos = PMA_getStartPosToDisplayRow($unlim_num_rows);
    }
    return $pos;
}
All Usage Examples Of PMA\libraries\Table::countRecords