所以我有一个数据库来记录参加 session 的人数。 该表设置如下:
name | status
----+---------
Joe | No
Jon | Yes
Mike | Yes
我想要做的是计算状态为"is"的次数,然后将其输出到屏幕。
$yes_attending = 'SELECT status FROM attendees WHERE "status=Yes"';
$yes_result= $link->query($yes_attending);
if($yes_result=mysqli_query($link,$yes_attending))
{
$yesCount = mysqli_num_rows($yes_result);
} else{
echo '0';
}
我的问题是,即使数据库中确实有与"is"条件匹配的信息,此代码也会返回 0。
最佳答案
您似乎误解了 mysqli_query()
的作用,无论执行与否,它都会返回。
$sql = "SELECT status FROM attendees WHERE status='Yes'";
if($rsl = mysqli_query($link,$sql) != false){
echo 'Query successfully executed';
if($c = mysqli_num_rows($rsl) > 0){
echo "The amount of results: '$c'";
} else {
echo "There are no attendees that have an active status";
}
} else {
printf("Query failed: %s\n", mysqli_error($link));
}
另外,您为什么要求选择状态? 我这么问是因为结果是"is"。
关于php/mysql 计算匹配条件的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36457989/