Palette - Make it Colorful🎨
Palette - Visual Page Builder, no design degree required.
Scroll
Query count()
12/04/2025, by Ivan
Menu
- Understanding Drupal
- Drupal System Requirements
- Installing Drupal 8
- Drupal 8 Update
- Drupal Site Administration
- Drupal 8 Modules
- Drupal Modules Comparison
- Drupal Themes
- Drupal field types
- Configuration Management
- Multisite Drupal
- Accessibility Features
- Troubleshooting in Drupal
- Creating modules for Drupal
-
Drupal 8 API
- Authenticatie-API
- Block API
- Cache API
- CKEditor API
- Configuration Management API
- Database API
- Entity API
- Theming in Drupal
- Upgrade to Drupal 11
Any query can have a corresponding "count query". A count query returns the number of rows in the original query. To get a count query from an existing query (which is a select query object implementing the SelectInterface), use the countQuery() method.
$count_query = $query->countQuery();
$count_query is now a new dynamic select query without order constraints, which when executed will return a result set with only one value — the number of records that would match the original query. Since PHP supports method chaining on returned objects, the following approach is common:
$num_rows = $query->countQuery()->execute()->fetchField();
For an entity query (implementing the QueryInterface), the code is slightly different:
$num_rows = $query->count()->execute();