Home >Database >Mysql Tutorial >Why Does `mysqli_num_rows()` Return a 'boolean given' Error in PHP and MySQL?

Why Does `mysqli_num_rows()` Return a 'boolean given' Error in PHP and MySQL?

Linda Hamilton
Linda HamiltonOriginal
2024-12-09 00:17:11209browse

Why Does `mysqli_num_rows()` Return a

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:

  • The query syntax has an error.
  • The mysqli_query() function is not properly executed.
  • The query returns an empty result set.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn