Mysqli_num_rows() expects parameter 1 to be mysqli_result object given
The reason for this error is that you may be passing the Show Moreover, what I see is that you're mixing up two different approaches in php for using mysql databases - the Object-Oriented Approach and the Procedural Approach. You have to chose one of them and then proceed something like this - Procedural Approach (Using mysqli extension)
Object-Oriented Approach (Using mysqli extension)
You can read more about it here - https://www.w3schools.com/php/php_mysql_select.asp Mysqli_num_rows() expects parameter 1 to be mysqli_result is one of the most basic kinds of errors that occur in PHP while using MySQLi functions.
If you are getting this error, don’t panic; there is nothing to worry about because it is not an error actually. Basically, “mysqli_num_rows() expects parameter 1 to be mysqli_result” is a warning which occurs while fetching data using MySQLi functions. When we use the mysqli_query() function with the SELECT query, it returns some resultant data from the database. We can use mysqli_fetch_array() or mysqli_fetch_assoc() function to fetch every resultant row. Example We have a table of students with two columns, “id” and “name.” and we have a PHP code to fetch the student name as below.
If we want to find the name of the student with id ‘1’, it will display the output as below. Output
The above PHP code is correct because there is a field with ‘id’ = 1 Now, if we try to fetch the name where ‘id’ = 4 Here the problem occurs of ‘Warning: Mysqli_num_rows() Expects Parameter 1 to Be Mysqli_result‘. This warning might appear on your PHP page or in the error.log file; it depends on your working environment. SolutionAs we can understand from the above explanation that mysqli_query() returns no data because there is no data record exist with ‘id’ = 4 but still program proceeds and runs the next query of mysqli_fetch_assoc(), which expects at least one row in $result variable to fetch data and it prompts a warning. To resolve that problem, we just have to check the number of results and only fetch data if there is at least 1 row in resultant data. SOLVED CODE
ConclusionI hope now you can resolve the mentioned error in your amazing PHP project. Happy Coding 🙂 |