Home >Database >Mysql Tutorial >Why Does `mysqli_num_rows()` Return a 'boolean given' Error in PHP and MySQL?
PHP & MySQL: Resolving mysqli_num_rows() Parameter Error with mysqli_result
When integrating HTML Purifier into PHP for data validation, it's crucial to ensure proper parameter passing to avoid common errors like "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given."
Understanding the Error
Typically, this error occurs when the mysqli_num_rows() function is called with an incorrect argument. It expects a valid mysqli_result object as its first parameter. In the given code snippet, the first parameter is $dbc, which needs to be a valid result set from a MySQL query.
Possible Causes
Incorrect usage or a malformed query can lead to a boolean value (FALSE) being returned instead of a mysqli_result object. This can happen if:
Resolving the Issue
In the provided code, the issue lies within the SQL query used to retrieve data from the database:
$dbc = mysqli_query($mysqli,"SELECT users.*, profile.* FROM users INNER JOIN contact_info ON contact_info.user_id = users.user_id WHERE users.user_id=3");
The error is caused by the omission of the join with profile in comparison to users and contact_info. The correct query should be:
$dbc = mysqli_query($mysqli,"SELECT u.*, p.* FROM users AS u LEFT JOIN profile AS p ON u.user_id = p.user_id WHERE u.user_id=3");
By fixing the query, the $dbc variable will return a valid mysqli_result object, enabling you to use mysqli_num_rows() effectively:
if (mysqli_num_rows($dbc) == 0) { //...
The above is the detailed content of Why Does `mysqli_num_rows()` Return a 'boolean given' Error in PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!