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'];
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();
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!