MySQLi count(*)始终返回1 [英] MySQLi count(*) always returns 1

查看:161
本文介绍了MySQLi count(*)始终返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算表中的行数,并认为这是执行此操作的正确方法:

I'm trying to count the number of rows in a table and thought that this was the correct way to do that:

$result = $db->query("SELECT COUNT(*) FROM `table`;");
$count = $result->num_rows;

但计数始终返回(int)1.如果我在phpMyAdmin中使用相同的查询,则会得到正确的结果.它位于一个表中,所以我也尝试测试$count[0],但是返回了NULL.

But counts always returns (int)1. If I use the same query in phpMyAdmin I get the right result. It sits in a table so I tried testing $count[0] as well, but that returns NULL.

正确的方法是什么?

推荐答案

您必须提取一条记录,其中将包含Count()的结果

You have to fetch that one record, it will contain the result of Count()

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

这篇关于MySQLi count(*)始终返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆