Home > Database > Mysql Tutorial > Why Does MySQLi's `COUNT(*)` Sometimes Return 1 Instead of the Actual Row Count?

Why Does MySQLi's `COUNT(*)` Sometimes Return 1 Instead of the Actual Row Count?

DDD
Release: 2024-12-10 01:52:08
Original
299 people have browsed it

Why Does MySQLi's `COUNT(*)` Sometimes Return 1 Instead of the Actual Row Count?

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];
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template