Home > Backend Development > PHP 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
Release: 2024-11-28 00:08:12
Original
686 people have browsed it

Why Does mysqli_num_rows() Return

mysqli_num_rows() Function Error in PHP and MySQL: A Detailed Guide

Issue:

When calling the mysqli_num_rows() function, the error "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given" is encountered. This error indicates a problem with the parameter passed to the function.

Analysis and Resolution:

The mysqli_num_rows() function expects the first parameter to be a valid mysqli_result object. In the provided code, $dbc is mistakenly used as the parameter. However, examining the code reveals that $dbc is the result of a mysqli_query() call.

The actual issue lies in the query itself. The query attempts to perform an INNER JOIN between the "users" and "profile" tables. However, there is no explicit JOIN condition specified in the query. This results in the query returning false, which is then passed to the mysqli_num_rows() function and triggers the error.

To resolve this issue, you need to specify a valid JOIN condition in the query. In this case, you should likely JOIN "users" and "profile" on their user_id fields. Once the query is corrected, it should return a valid mysqli_result object, which can then be passed to the mysqli_num_rows() function.

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
");
Copy after login

With this adjustment, the query should execute successfully, returning a mysqli_result object that can be used by the mysqli_num_rows() function without triggering the error.

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template