mysqli_affected_rows() expects parameter 1 to be m

2020-07-21 06:07发布

问题:

I am working on making server side validation for a form. Using AJAX, the form sends the value in the input field for 'username' to my php page which then checks to see if this username already exists in the database.

Here is my php code:

$result = mysqli_query($dblink, "SELECT * FROM users WHERE `username` = '$regname'") 
or die(mysqli_error($dblink));
echo mysqli_affected_rows($result);

*(At the moment I am doing a simple echo for the mysqli_affected_rows just to see if my MySQL query is working as intended)*

The error I am getting is:

Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, object given in /Users/test/Sites/proj/formvalidate.php on line 20

I am not quite sure what this error is trying to tell me. From what I have Googled "object" is a reference to OOP programming methods, but (as far as I know) I am not using OOP concepts/principles in this particular example? Or did I misinterpret this error message?

Thank you.

回答1:

Rather than passing $result in to mysqli_affected_rows you actually want to pass the DB link (returned by mysqli_connect) which will give you the number of rows affected by the previous query. See:

http://uk.php.net/mysqli_affected_rows



回答2:

echo mysqli_affected_rows($dblink);

The mysqli object contains the count of the affected rows not the result set. I recommend you to use mysqli with OO style or try PDO.



回答3:

You should only give the $mysqli_link (in your case $dblink) , not the $result in the code that you have written.



回答4:

echo mysqli_num_rows($result);

This should work.



标签: php mysqli