Skip to content

Instantly share code, notes, and snippets.

@biplobice
Last active August 28, 2025 02:47
Show Gist options
  • Save biplobice/f13038e5ff335688b69f316a5bfc742e to your computer and use it in GitHub Desktop.
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
<?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";
}
}
@biplobice
Copy link
Author

Run with ./concrete/bin/concrete c5:exec db_row_counts.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment