I'm trying to get the total number of rows returned by my query before I limit it. I'm attempting to use the advice found here, which uses: SQL_CALC_FOUND_ROWS
and then FOUND_ROWS()
in a second query;
They give this example:
SELECT SQL_CALC_FOUND_ROWS something FROM your_table WHERE whatever;
SELECT FOUND_ROWS( ) ;
What I can't figure out is how to capture the second value from FOUND_ROWS()
into a PHP variable. I'm working within Wordpress, so I'm using the global $wpdb to handle MySQL interactions. Here's a simplified version of what I'm trying:
$rows = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS something FROM your_table WHERE whatever;" );
$count = $wpdb->get_results( "SELECT FOUND_ROWS();" );
The first query works, but $count is always empty. If I run them both in phpMyAdmin, the queries run without error.
I've also tried this:
$rows = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS something FROM your_table WHERE whatever;
SET @count = FOUND_ROWS();" );
...but it fails entirely.
What am I doing wrong?