Skip to content

Commit d56c4a1

Browse files
Nandan108claude
andcommitted
Fix FOUND_ROWS() for more than nine rows without SQL_CALC_FOUND_ROWS
After a SELECT without SQL_CALC_FOUND_ROWS or after a SHOW statement, FOUND_ROWS() counts the rows of the previous query. The count was read with fetchColumn()[0], which takes the first character of a stringified count, so FOUND_ROWS() returned "1" for 12 rows. Without stringified fetches, indexing the integer count returned null, and the result was 0. Use the fetched count directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6546e45 commit d56c4a1

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5161,12 +5161,12 @@ private function translate_function_call( WP_Parser_Node $node ): string {
51615161
} elseif ( is_string( $found_rows ) ) {
51625162
return (int) $this->execute_sqlite_query(
51635163
sprintf( 'SELECT COUNT(*) FROM (%s)', $found_rows )
5164-
)->fetchColumn()[0];
5164+
)->fetchColumn();
51655165
} elseif ( is_array( $found_rows ) && isset( $found_rows[0] ) ) {
51665166
return (int) $this->execute_sqlite_query(
51675167
sprintf( 'SELECT COUNT(*) FROM (%s)', $found_rows[0] ),
51685168
$found_rows[1] ?? array()
5169-
)->fetchColumn()[0];
5169+
)->fetchColumn();
51705170
} else {
51715171
return 0;
51725172
}

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,6 +4186,24 @@ public function testCalcFoundRows() {
41864186
);
41874187
}
41884188

4189+
public function testFoundRowsWithoutSqlCalcFoundRowsAboveNine(): void {
4190+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
4191+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12)' );
4192+
for ( $i = 1; $i <= 10; $i++ ) {
4193+
$this->assertQuery( "CREATE TABLE t$i (id INT)" );
4194+
}
4195+
4196+
// SELECT
4197+
$this->assertQuery( 'SELECT * FROM t' );
4198+
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4199+
$this->assertSame( '12', $result[0]->{'FOUND_ROWS()'} );
4200+
4201+
// SHOW TABLES ("_dates", "_options", "t", and "t1" to "t10")
4202+
$this->assertQuery( 'SHOW TABLES' );
4203+
$result = $this->assertQuery( 'SELECT FOUND_ROWS()' );
4204+
$this->assertSame( '13', $result[0]->{'FOUND_ROWS()'} );
4205+
}
4206+
41894207
public function testFoundRowsWithoutSqlCalcFoundRows(): void {
41904208
$this->assertQuery( 'DROP TABLE _dates' );
41914209
$this->assertQuery( 'DROP TABLE _options' );

0 commit comments

Comments
 (0)