MySQLi count(*) 总是返回 1 [英] MySQLi count(*) always returns 1

查看:35
本文介绍了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.

这样做的正确方法是什么?

What is the right way to do this?

推荐答案

你必须获取那一条记录,它会包含 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天全站免登陆