mysql - PHP Database query result error

663

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.

53

Answer

Solution:

You should add an extra column to yourwhere 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...

616

Answer

Solution:

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;
715

Answer

Solution:

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;
}
564

Answer

Solution:

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.

598

Answer

Solution:

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 amysqli_result and not anobject

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.

People are also looking for solutions to the problem: php - Functions - Model, Controller

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.