我正在用PHP开发一个小项目。我试图获取与数据库中类别相对应的数字以显示,但在显示页面上多次出现相同的错误:
警告:mysqli_num_rows()期望参数1为mysqli_result,在第34行的C:\ xampp \ htdocs(A)Book 2.0 \ Bootstrap \ content_function.php中给出布尔值
以下是该错误针对的代码:
function getnumtopics($cat_id, $subcat_id){
include ('../db.php');
$select = mysqli_query($mysqli, "SELECT category_id, subcategory_id FROM topics WHERE".$cat_id." = category_id
AND ".$subcat_id." = subcategory_id");
return mysqli_num_rows($select);
}
第34行是return语句。我的语法似乎有问题吗?
答案 0 :(得分:3)
您应先检查$select(result)
的结果(即mysqli_query
),然后再将其传递给mysqli_num_rows
,请参阅documentation
答案 1 :(得分:2)
问题在这里:
"SELECT category_id, subcategory_id FROM topics WHERE".$cat_id." = category_id AND ".$subcat_id." = subcategory_id"
查询中有两个问题,首先是您忘记在WHERE
之后加上空格,其次是在与字符串比较时,必须将其与单引号连接起来,例如:
"SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = 'category_id' AND ".$subcat_id." = 'subcategory_id'";
尝试上述查询。
答案 2 :(得分:2)
尝试一下:
function getnumtopics($cat_id, $subcat_id){
include ('../db.php');
$select = mysqli_query($mysqli, "SELECT category_id, subcategory_id FROM topics WHERE ".$cat_id." = category_id
AND ".$subcat_id." = subcategory_id");
$get = mysqli_num_rows($select);
return $get;