Home > Backend Development > PHP Tutorial > Why Does My MySQLi Query Only Return One Row?

Why Does My MySQLi Query Only Return One Row?

Susan Sarandon
Release: 2024-11-14 12:11:01
Original
358 people have browsed it

Why Does My MySQLi Query Only Return One Row?

MySQLi Query Only Returns One Row

In the provided code, the author attempts to fetch multiple rows from the database using MySQLi, but the query only returns a single row. This is despite confirming that the SQL query returns two rows when executed directly in phpMyAdmin.

The Issue

The issue lies in the method used to fetch data from the MySQLi result object. In the code, fetch_array() is used, which retrieves only the first row of the result set. To retrieve all rows, fetch_all() should be used instead.

Corrected Code

$request_list_result = $mysqli->query("
SELECT buddy_requester_id, buddy_reciepient_id, user_id, user_fullname FROM sb_buddies
JOIN sb_users ON buddy_requester_id=user_id
WHERE buddy_status='0' AND buddy_reciepient_id='" . get_uid() . "'");

$request_list = $request_list_result->fetch_all();

// Now $request_list holds all rows in the result set
Copy after login

Additional Resources

For more information on fetching data from MySQLi result sets, refer to the following resource:

  • [mysqli_result::fetch_all()](https://www.php.net/manual/en/mysqli-result.fetch-all.php)

The above is the detailed content of Why Does My MySQLi Query Only Return One Row?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template