mysqli_num_rows() 期望参数 1 是 mysqli_result,对象

2021-12-25 00:00:00 php mysqli

这是我第一次使用mysqli.它似乎在 mysqli_num_rows() 中的括号之间寻找结果集的名称.然而,当我尝试 $stmt、$conn 和什么都没有时,我得到了同样的错误.令人沮丧!下面最后一行中的 $WHAT 是什么?

This is my first experience with mysqli. It seems to be looking for the name of a result set in between the parentheses in mysqli_num_rows(). Yet when I tried $stmt, $conn, and nothing, I got the same error. Frustrating! What goes where $WHAT is in the last line below?

或者我尝试的方式不对.我想要做的就是检查是否返回了结果.我真的不需要行数.我应该只做一个带有错误消息的 else 语句吗?这是最好的方法吗?是否有一种编写函数来连接和接受查询及其参数的好方法?我为 mysql 写了一个,但这太不同了!我不期待重写几十个查询!

Or maybe I'm trying the wrong tack. All I want to do is check that a result was returned. I don't really need the number of rows. Should I just do an else statement with an error message? Is that the best way to do it? And is there a good way to write a function to connect and accept the query and it's parameters? I wrote one for mysql but this is so different! I'm not looking forward to rewriting dozens of queries!

$conn = mysqli_connect($host, $user, $pwd, $db,$port=$port_nbr); 

if ($mysqli_connect_errno) {
    printf("Connect failed: %s
",
    mysqli_connect_error());
    exit;
}
if($stmt=$conn->prepare("SELECT id, name, status, type FROM organization")) {
    $stmt->execute();
    $stmt->bind_result($org_id, $orgname, $orgstatus, $orgtype);    
    $num=mysqli_num_rows($WHAT);
}

推荐答案

当您只想要面向对象时,您正在结合面向过程和面向对象的方法.改变

You're combining procedural and object oriented approaches, when you only want object oriented. Change

$num=mysqli_num_rows($WHAT);

$num = $stmt->num_rows();

相关文章