I think the cleanest looking / most direct way would be to go through the information_schema
.
$db = JFactory::getDBO();
try {
/* SELECT table_name, table_rows
* FROM information_schema.tables
* WHERE table_name LIKE '%password%'
*/
$query = $db->getQuery(true)
->select("table_name, table_rows")
->from("information_schema.tables")
->where("table_name LIKE '%password%'");
$db->setQuery($query);
$db->execute();
$resultset = $db->loadAssocList();
if (!$resultset) {
echo "No Tables Containing 'password' Found";
} else {
echo "<table>";
echo "<tr><th>Table Name</th><th>Row Count</th></tr>";
foreach ($resultset as $row) {
echo "<tr><td>{$row['table_name']}</td><td>{$row['table_rows']}</td></tr>";
}
echo "</table>";
}
} catch (Exception $e) {
echo "Syntax Error: " , $e->getMessage(); // never show the actual error message to the public
}
Output (with my fabricated test tables):
----------------------------------
| Table Name | Row Count |
|----------------|---------------|
| a_passwords_1 | 3 |
|----------------|---------------|
| a_passwords_2 | 5 |
|----------------|---------------|
| a_passwords_3 | 0 |
----------------------------------
While this is 100% accurate for my database, I should include a link that offers a bit more detail about the fineprint and performance comparisons between a few different approaches.
Also, I want to state that when the raw query was executed via phpMyAdmin, many warning flooded the screen, but none of that mess was presented when called from php/Joomla.
Of course, if you are only interested in the aggregate count, this will be more direct:
$query = $db->getQuery(true)
->select("SUM(table_rows)")
->from("information_schema.tables")
->where("table_name LIKE '%password%'");
$db->setQuery($query);
$db->execute();
echo $db->loadResult();
Output:
8