Why MySQLi count(*) Always Returns 1
When counting the number of rows in a MySQL table using the MySQLi extension, the COUNT(*) function may return 1 instead of the expected count.
To resolve this issue, it's essential to fetch the result of the query. The following revised code demonstrates the correct approach:
// Execute the query $result = $db->query("SELECT COUNT(*) FROM `table`"); // Fetch the result row $row = $result->fetch_row(); // Extract the count from the fetched row index 0 $count = $row[0];
By fetching the row and accessing its first index, you can retrieve the actual count of rows in the table. This method will yield the correct result, even when COUNT(*) initially returns 1.
The above is the detailed content of Why Does MySQLi's `COUNT(*)` Sometimes Return 1 Instead of the Actual Row Count?. For more information, please follow other related articles on the PHP Chinese website!