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

Why Does My MySQLi Query Only Return One Row When I Expect Two?

Susan Sarandon
Release: 2024-11-17 07:14:03
Original
348 people have browsed it

Why Does My MySQLi Query Only Return One Row When I Expect Two?

MySQLi Query Fetching Single Row

This code snippet aims to retrieve two rows of data from a MySQL table but only returns one. The SQL statement in phpMyAdmin successfully returns both rows, but the code in question fails to do so:

$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_row = $request_list_result->fetch_array();

echo $request_list['user_fullname'];
Copy after login

The issue lies in the use of fetch_array, which retrieves only a single row as an array. To fetch all rows from the result, the intended function is fetch_all:

$request_list = $request_list_result->fetch_all();
Copy after login

This function returns an array of arrays, where each inner array represents a row in the result set. Using fetch_all will allow the code to access both rows correctly.

For reference, the documentation on fetch_all can be found here: http://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 When I Expect Two?. 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