Home >Database >Mysql Tutorial >Why Does My MySQLi COUNT(*) Query Return 1 Instead of the Actual Row Count?

Why Does My MySQLi COUNT(*) Query Return 1 Instead of the Actual Row Count?

DDD
DDDOriginal
2024-12-09 10:17:10835browse

Why Does My MySQLi COUNT(*) Query Return 1 Instead of the Actual Row Count?

MySQLi Count(*) Query Incorrectly Returns One

When attempting to determine the number of rows within a table using the MySQLi extension, a user encountered an issue where the count() function consistently returned the value 1. Despite executing the same query in phpMyAdmin produced the intended result, the $count[0] method returned the value NULL.

Correct Implementation

To rectify this issue, it is crucial to retrieve the sole record returned by the count() query, as it encapsulates the desired result. The following code snippet illustrates the correct implementation:

$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];

The above is the detailed content of Why Does My MySQLi COUNT(*) Query Return 1 Instead of the Actual Row Count?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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