简体   繁体   中英

PHP Database query result error

Well, basically here is the error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in

Here is my code:

function user_banned ($con, $username) {
    $result = mysqli_query($con, "SELECT `banned` FROM `users` WHERE `username` = '$username'");
    return(mysqli_num_rows($con) == 1) ? true : false;
}

I am not too sure what is causing this, I have seen several questions about this issue, however none of them have assisted my in finding a way to fix this issue.

Procedural style int mysqli_num_rows ( mysqli_result $result )

http://php.net/manual/en/mysqli-result.num-rows.php

You are passing the conn it should be as

return(mysqli_num_rows($result) == 1) ? true : false;

You should add an extra column to your where clause: AND banned='yes' as an example.

Plus, you're using the wrong variable in

return(mysqli_num_rows($con) == 1) ? true : false;

you need to use the query variable and not the connection

return(mysqli_num_rows($result) == 1) ? true : false;

which explains the initial error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given in...

Try this:

function user_banned ($con, $username) {
    $result = mysqli_query($con, "SELECT `banned` FROM `users` WHERE `username` = '$username'");
    return(mysqli_num_rows($result) == 1) ? true : false;
}

You dont want to pass in the connection, you need to pass in the result of the query.

function user_banned ($con, $username) {
    $result = mysqli_query($con, "SELECT `banned` FROM `users` WHERE `username` = '$username'");
    return(mysqli_num_rows($con) == 1) ? true : false;
}

needs to have the return be:

return (musqli_num_rows($result) == 1) ? true : false;

Your error says exactly that. It is looking for a mysqli_result and not an object

Your actual error is based around things returning true because you are using the wrong query. Ill follow your example on the returned select and say: SELECT 'banned' from 'users' WHERE 'username' = '$username' AND 'banned' IS TRUE

this is assuming banned is a bool of course, and not a string, bit, int, etc.

Did you try with $result instead of $con for mysqli_num_rows ?

function user_banned ($con, $username) {
$result = mysqli_query($con, "SELECT `banned` FROM `users` WHERE `username` =     '$username'");
return(mysqli_num_rows($result) == 1) ? true : false;
}

You should also check if your connection to your database is good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM