/** * @param string $tableName * @param Condition $criteria * @return int */ public static function countMatching($tableName, Condition $criteria = null) { $criteriaSQL = $criteria != null ? $criteria->toSQL() : "1"; // we assume id because Record always has id $sql = "SELECT count(`id`) AS 'count' FROM `" . addslashes($tableName) . "` WHERE " . $criteriaSQL; $stmt = DB::dbh()->prepare($sql); if ($criteria != null) { $criteria->bind($stmt); } try { $stmt->execute(); } catch (PDOException $ex) { throw new TableGatewayException($ex, FrameEx::HIGH); } $row = $stmt->fetch(PDO::FETCH_ASSOC); return $row['count']; }