0

Here's my current code:

<?php
$identifiers_link = mysql_connect("localhost", "XXXXX", "XXXXX");
mysql_select_db("XXXXX", $identifiers_link);

$count_0 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_0", $identifiers_link);
$count_1 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_1", $identifiers_link);
$count_2 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_2", $identifiers_link);
$count_3 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_3", $identifiers_link);
$count_4 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_4", $identifiers_link);
$count_5 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_5", $identifiers_link);
$count_6 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_6", $identifiers_link);
$count_7 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_7", $identifiers_link);
$count_8 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_8", $identifiers_link);
$count_9 = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_9", $identifiers_link);
$count_a = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_a", $identifiers_link);
$count_b = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_b", $identifiers_link);
$count_c = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_c", $identifiers_link);
$count_d = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_d", $identifiers_link);
$count_e = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_e", $identifiers_link);
$count_f = mysql_query("SELECT COUNT(hash_id) FROM apis_hashes_f", $identifiers_link);

$identifiers_count = $count_0 + $count_1 + $count_2 + $count_3 + $count_4 + $count_5 + $count_6 + $count_7 + $count_8 + $count_9 + $count_a + $count_b + $count_c + $count_d + $count_e + $count_f;

echo $identifiers_count;

?>

What I'm trying to do is add up how many rows are in several tables.

The problem I'm having is that every time I refresh the page, the number is either 200 or 216. However, the actual number of rows it should be showing is 14.

I have no idea what could be causing this to happen, I've run each of the queries directly in phpMyAdmin and they show the correct results. Any idea?

EDIT: In case it makes a different, some of the tables are currently empty.

James
  • 75
  • 5
  • possible duplicate of [COUNT(\*) from multiple tables in MySQL](http://stackoverflow.com/questions/3761240/count-from-multiple-tables-in-mysql) – Sheepy Feb 08 '15 at 16:46
  • 1
    Using `mysql_` is deprecated. Use `mysqli` or `PDO` instead. – worldofjr Feb 08 '15 at 16:47

2 Answers2

0

If you need an estimate you could try something like this:

SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'apis_hashes_%';
JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57
0

You are using count so you have to run a loop. Your every query should look like this

$query_0 = mysql_query("SELECT COUNT(hash_id) as count FROM apis_hashes_0", $identifiers_link);
while($row=mysql_fetch_array($query_0)){
$count_0 = $row['count'];
}

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
  • He won't need the while I don't think because it should only return one record, no? – chris85 Feb 08 '15 at 16:53
  • @chris85 According to me OP should run a loop as one can only get the value of count in a loop or using array_keys. By the way + 1 for your answer – Utkarsh Dixit Feb 08 '15 at 16:55
  • Count should only return one record though so I think just `$row=mysql_fetch_array($query_0)` and `$count_0 = $row['count'];` are needed. or maybe even initialize $count as 0 then `$count += $row['count'];` – chris85 Feb 08 '15 at 17:04
  • @chris85 You are saying right, i am understanding what you are saying – Utkarsh Dixit Feb 08 '15 at 17:11