Question:
PHP/MySQL how to count number of rows in entire database?
Patrick
2011-12-08 14:52:25 UTC
I have a MySQL database, with roughly 25-30 tables. Each table has about 4 columns, but anywhere from 10-80 rows. How can I use php to print count the number of rows in all the tables?
I've tried;

$sql_query = "SELECT * FROM laptops, desktops, netbooks";
$result = mysql_query($sql_query);
$rows = mysql_num_rows($result);
echo $rows;

with no luck...
Ideas?
Five answers:
Mark O
2011-12-08 14:58:52 UTC
i=0;

$tables=array("Table1","Table2","Table3","Table4");

while($i<=sizeof($tables))

{

$totalrecnum = mysql_query("SELECT * FROM {$tables[i]}");

$num_rows = mysql_num_rows($totalrecnum);

$totalrows = $totalrows + $num_rows;

i++;

}

echo $totalrows;





hmmmmmm



build an array of the table names then loop through the arry counting each
?
2016-12-08 13:45:52 UTC
Count Php Mysql
Serge M
2011-12-09 06:38:40 UTC
select count(*)

from (

select 1 from laptops

union all

select 2 from desktops

union all

select 3 from netbooks

/* ... */

) X
jang
2016-10-18 02:22:42 UTC
there is in all likelihood an a lot less complicated way, yet you are able to reproduction the sheet and paste it right into a sparkling one. it is going to in undemanding words paste the cells that were exhibiting contained in the filtered sheet and also you may see what percentage rows there are then delete that sheet
TheMadProfessor
2011-12-09 10:59:01 UTC
SELECT count1 + count2 + count3 AS totalCount

FROM (SELECT COUNT(*) AS count1 FROM laptops),

(SELECT COUNT(*) AS count2 FROM desktops),

(SELECT COUNT(*) AS count3 FROM netbooks)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...