选择计数错误 mysqli_num_rows() 期望参数 1 mysqli_result,给定布尔值



>我正在尝试从按project_id分组的每个项目的列投票中计算"是"票。 vote 可以是"是"、"否"和空值,这是一个布尔值。 我使用了"喜欢"运算符。 但它抛出错误这是我的表

这是代码

$sql = "SELECT COUNT(vote) as count,project_id FROM tbl_vote group by   project_id where vote like '_Y'";
$result = $conn->query($sql);  if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "count: " . $row["count"]. " project_id:".$row["project_id"]."    <br>";
}
} 
else {
echo "0 results";
}

错误在 if 条件中

警告:mysqli_num_rows(( 期望参数 1 mysqli_result,布尔值在第 18 行的 C:\xampp\htdocs\selection\compute_sums.php 中给出

你在 where 子句之前有按子句放置分组

$sql = "SELECT COUNT(vote) as count,project_id FROM tbl_vote where vote = 'Yes' group by   project_id";

SQL 子句的顺序如下:

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
SQL_NO_CACHE [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
[HAVING where_condition]
[WINDOW window_name AS (window_spec)
[, window_name AS (window_spec)] ...]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[INTO OUTFILE 'file_name'
[CHARACTER SET charset_name]
export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR {UPDATE | SHARE} [OF tbl_name [, tbl_name] ...] [NOWAIT | SKIP LOCKED] 
| LOCK IN SHARE MODE]]

请参考: https://dev.mysql.com/doc/refman/8.0/en/select.html

相关内容

  • 没有找到相关文章

最新更新