Home >Database >Mysql Tutorial >Why does `mysqli_num_rows()` return 'expects parameter 1 to be mysqli_result, boolean given'?

Why does `mysqli_num_rows()` return 'expects parameter 1 to be mysqli_result, boolean given'?

DDD
DDDOriginal
2024-12-08 18:00:34571browse

Why does `mysqli_num_rows()` return

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

Error Analysis

The error message "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given" arises when trying to use the mysqli_num_rows() function on a boolean value instead of a valid MySQL result set (mysqli_result).

Source of the Error

In the provided code, the error occurs on line 22:

if (mysqli_num_rows($dbc) == 0) {

Debugging the Issue

The variable $dbc is assigned on line 13 using the mysqli_query() function. However, examining the query reveals an error:

$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 query attempts to join the "users" and "profile" tables using an "INNER JOIN," but there is no join condition between "profile" and any other table. As a result, the query returns false (boolean false).

Resolution

To resolve the error, the query must be corrected to include a proper join condition. Here is the corrected query:

$dbc = mysqli_query($mysqli,"SELECT users.*, profile.*
                                 FROM users 
                                 INNER JOIN profile 
                                 ON users.user_id = profile.user_id
                                 WHERE users.user_id=3");

With this correction, the query will return a valid MySQL result set, allowing mysqli_num_rows() to operate correctly.

The above is the detailed content of Why does `mysqli_num_rows()` return 'expects parameter 1 to be mysqli_result, boolean given'?. 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