Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOUND_ROWS() keeps returning 0

Tags:

php

select

count

$result = $db_con->query("SELECT SQL_CALC_FOUND_ROWS * FROM users LIMIT 0,10");

$count_result = $db_con->query("SELECT FOUND_ROWS() as totalcount");
$row = $count_result->fetch_assoc();
$total = $row['totalcount'];

$total keeps returning 0 even though the $result contains all the user rows in it.

ini_set("mysql.trace_mode", "0");

I found out about this setting via searching a similar Q on here, but it did not resolve my issue.

When I use the 2 select statements in my MySQL client it always outputs the right count. Just can't seem to figure out how to get it working on my php page.

PHP Version: 5.2.5 build 6 MySQL Version: 5.1.30

Note: The code I provided here is not being used on my actual project, however, it does exemplify the problem I am having with not being able to retrieve the total count. This code, from what I've read about found_rows(), should work but it doesn't.

like image 708
Seth Avatar asked Feb 19 '26 08:02

Seth


1 Answers

On php.net I found someone with maybe the same problem. He is speaking of a race condition that could occur: php.net. Basically he solves this problem by temporary locking the table at the cost of slowing things down.

like image 158
merkuro Avatar answered Feb 20 '26 20:02

merkuro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!