How to debug php/MySQL COUNT(id) returning 1 instead of total entries value

Multi tool use
How to debug php/MySQL COUNT(id) returning 1 instead of total entries value
I need the total number of entries in my database.
Question: Why does the line "print_r($rows)" return "Array ( [COUNT(id)] => 79 )" but then the line "$recordCount = Count($row); echo "
record count = " . $recordCount;" returns "record count = 1".
I've tried a massive amount of different versions of this code, but I cant type them all here or it would take forever to read. Here is the current version of the code, which is giving me the unexpected results I mention above:
// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM games ORDER BY id DESC LIMIT $startRow, $rowsPerPage";
$result = $conn->query($sql);
$sql = "SELECT COUNT(id) FROM games";
$results = $conn->query($sql);
$row = $results->fetch_assoc();
print_r($row);
$recordCount = Count($row);
echo "<br/> record count = " . $recordCount;
$totalPages = ceil($recordCount / $rowsPerPage);
$pagination = "<div class='pagination'>";
for ($i = 0; $i <= $totalPages; $i++) {
$pagination .= "<a href='index.php?page=" . $i . "'>" . $i . "</a>";
echo 'wtf!';
}
$pagination .= "</div>";
echo ' <br/> pagination = ' . $pagination;
THANKS TO YOU HEROES I NOW HAVE IT CORRECT: Here incase someone else in 2018 is reading online and finding lots of incorrect implementations of it:
$sql = "SELECT COUNT(id) as countid FROM games";
$results = $conn->query($sql);
$row = $results->fetch_assoc();
$recordCount = $row["countid"]; // the countid gives me the total i expected :DDDD
echo "<br/> record count = " . $recordCount;
SHOW CREATE TABLE games
Count($row)
is the number of rows returned - not the content of the rows, try echo $row["COUNT(id)"];
– Nigel Ren
Jun 30 at 12:47
Count($row)
echo $row["COUNT(id)"];
The
$row
is an array that only contains 1 values which is [COUNT(id)] => 79
. I would suggest you change your query to something like SELECT COUNT(id) total_games FROM games
and then access the result using echo $row['total_games'];
– Cyclonecode
Jun 30 at 12:48
$row
[COUNT(id)] => 79
SELECT COUNT(id) total_games FROM games
echo $row['total_games'];
using a alias in the query is eazier..
COUNT(id) as id_count
and use $row['id_count']
in PHP.. Using $row["COUNT(id)"];
is a bit tricky in PHP @NigelRen– Raymond Nijland
Jun 30 at 12:48
COUNT(id) as id_count
$row['id_count']
$row["COUNT(id)"];
ah ok i think i understand :DD ... also I did try using the alias thing, but said something like "invalid index for array" when i ran it
– Super MegaBroBro
Jun 30 at 12:48
2 Answers
2
row
is an associative array with an entry for every column in the result set. Since there's only one column there, count($row)
returns 1
. Instead, you should just access the only column there:
row
count($row)
1
$row = $results->fetch_assoc();
$recordCount = $row["COUNT(id)"];
amazing man thank you. I've been fiddling around beating around that bush for ages and I almost had it. As a sidenote this answer is slightly wrong. I will adjust my question to show th eanswer and mark yours correct as this and the other commenters above is what made me find the correct answer
– Super MegaBroBro
Jun 30 at 12:51
i believe it should be
COUNT(id)
it matters how it's been used in the query.. if the query is using COUNT(id)
the PHP code should also use COUNT(id)
.. it's case sensitive– Raymond Nijland
Jun 30 at 12:51
COUNT(id)
COUNT(id)
COUNT(id)
@RaymondNijland yes, that's right. Momentary lapse of concentration - edited and fixed.
– Mureinik
Jun 30 at 12:52
Well i wasn't completely 100% sure but it looked logical to me. Because i always use aliases in these situations when using MySQL functions i never used it like this,,
– Raymond Nijland
Jun 30 at 12:54
for me i couldnt get it working , without using the alias technique and passing that as the index to $row; --but you still solved all my confusion with this answer
– Super MegaBroBro
Jun 30 at 13:13
Yes, you could either aliase the column so that you can then use it as a key in associative array or since you already know that this statement:
1.$results = $conn->query($sql); looks something like this:
+-----------+
| count(id) |
+-----------+
| 13 |
+-----------+
1 row in set (0.01 sec)
2.$row = $results->fetch_assoc();returns an array with 1 element
So, you can just write $recordCount = $row[0]; to fetch that count.
The above answer is correct as well, but wanted to let you know about this direct, alternate approach in case you forget to aliase for whatever reason!
thanks for your help. I got it working but not sure I fully understood it all
– Super MegaBroBro
4 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Post the table structure
SHOW CREATE TABLE games
– Raymond Nijland
Jun 30 at 12:45