Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

php - mysqli_num_rows() expects parameter 1 to be mysqli_result, object

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?

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

$num=mysqli_num_rows($WHAT);

to

$num = $stmt->num_rows();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...