php MySQLi count(*) 总是返回 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3613074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:25:05  来源:igfitidea点击:

MySQLi count(*) always returns 1

phpmysqlmysqli

提问by Lode

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;

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.

但计数总是返回(int)1。如果我在 phpMyAdmin 中使用相同的查询,我会得到正确的结果。它位于一张桌子上,所以我也尝试进行测试$count[0],但返回NULL.

What is the right way to do this?

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

回答by VolkerK

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

您必须获取该记录,它将包含 Count() 的结果

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

回答by ErVeY

Always try to do an associative fetch, that way you can easy get what you want in multiple case result

始终尝试进行关联提取,这样您就可以在多种情况下轻松获得您想要的结果

Here's an example

这是一个例子

$result = $mysqli->query("SELECT COUNT(*) AS cityCount FROM myCity")
$row = $result->fetch_assoc();
echo $row['cityCount']." rows in table myCity.";

$result->close();

回答by Stephane Paquet

$result->num_rows; only returns the number of row(s) affected by a query. When you are performing a count(*) on a table it only returns one row so you can not have an other result than 1.

$result->num_rows; 只返回受查询影响的行数。当您在表上执行 count(*) 时,它只返回一行,因此您不能有除 1 之外的其他结果。

回答by Mike Causer

I find this way more readable:

我发现这种方式更具可读性:

$result = $mysqli->query('select count(*) as `c` from `table`');
$count = $result->fetch_object()->c;
echo "there are {$count} rows in the table";

Not that I have anything against arrays...

并不是说我反对数组...

回答by David Sargent

This worked well for me.

这对我来说效果很好。

// Veh Pro Count
$query_tvp = "SELECT count(*) as total from submit";
if ($result_tvp = $mysqli->query("$query_tvp")) {
    /* determine number of rows result set */
    $total_tvp = $result_tvp->fetch_row();
    $total_tvp = $total_tvp['0'];
    /* close result set */
    $result_tvp->close();
}

echo "Total: $total_tvp";

回答by alex

It's very simple:

这很简单:

$query = "select count(*)as count from users";
$result = $connection->query($query);
$count = $result->fetch_assoc()["count"];
echo $count;

回答by N1ckfm

For me this works, this is when you want to count a repeated elements inside a register.

对我来说这是有效的,这是当你想计算寄存器内重复元素的时候。

For example: number of people who mark 'yes' in a form.

例如:在表格中标记“是”的人数。

table: users

表:用户

register: vote

登记:投票

element of register: yes / no

寄存器元素:是/否

$sql_user = "SELECT COUNT(*) FROM users WHERE vote = 'yes'";    
$result1 = $mysqli->query($sql_user);    
$row1 = $result1->fetch_row();    
$n_yes = $row1['0'];