Last active
August 28, 2025 02:47
-
-
Save biplobice/f13038e5ff335688b69f316a5bfc742e to your computer and use it in GitHub Desktop.
A simple PHP script to count the number of rows in all tables of a Concrete CMS database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @author: Biplob Hossain <biplob.me> | |
| */ | |
| defined('C5_EXECUTE') or die("Access Denied"); | |
| use Concrete\Core\Database\Connection\Connection; | |
| use Concrete\Core\Support\Facade\Facade; | |
| $app = Facade::getFacadeApplication(); | |
| /** @var Connection $db */ | |
| $db = $app->make(Connection::class); | |
| // Get all tables (v8-compatible version of fetchFirstColumn) | |
| $rawTables = $db->fetchAll("SHOW TABLES"); | |
| $tableNames = array_map(function ($row) { | |
| return array_values($row)[0]; | |
| }, $rawTables); | |
| // Set display widths | |
| $tableColWidth = 64; | |
| $countColWidth = 10; | |
| // Print header | |
| echo str_pad("Table Name", $tableColWidth) . " | " . str_pad("Row Count", $countColWidth, ' ', STR_PAD_LEFT) . "\n"; | |
| echo str_repeat("-", $tableColWidth) . "-|-" . str_repeat("-", $countColWidth) . "\n"; | |
| // Loop through tables | |
| foreach ($tableNames as $table) { | |
| try { | |
| $count = $db->fetchColumn("SELECT COUNT(*) FROM `$table`"); | |
| echo str_pad($table, $tableColWidth) . " | " . str_pad($count, $countColWidth, ' ', STR_PAD_LEFT) . "\n"; | |
| } catch (Exception $e) { | |
| echo str_pad($table, $tableColWidth) . " | ERROR: " . $e->getMessage() . "\n"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
./concrete/bin/concrete c5:exec db_row_counts.php