Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of rows from a SELECT query? Error: mysqli_affected_rows() expects parameter 1 to be mysqli, object given

Tags:

php

mysqli

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.

like image 718
BrokenCode Avatar asked Jan 18 '23 09:01

BrokenCode


1 Answers

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

like image 197
pjumble Avatar answered Jan 20 '23 15:01

pjumble