5

This code only returns one row, but it should return 2 rows. I have tried the SQL in phpMyAdmin and it perfectly returned 2 rows. What am I doing wrong here?

$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'];

By the way, the code above is getting process to profile.php via following script:

$index = new Template('views/template/one.php', array(
'subtitle' => 'Dashboard',
'stylesheets' => array('/assets/css/profile.css'),
'scripts' => array('/assets/js/dashboard.js'),
'sidebar' => 'sidebar.php',
'content' => 'views/profile.php',
'errors' => $errors,
'successes' => $successes,
'request_list' => $request_list_row //right here
), true);

3 Answers 3

8
 mysqli_result::fetch_array

only fetches a single row as an array, you are looking for

 mysqli_result::fetch_all

to fetch all the rows.

More info here

http://php.net/manual/en/mysqli-result.fetch-all.php

1
  • If i use fetch_all like following: $request_list_result->fetch_all(); It says: Call to undefined method mysqli_result::fetch_all()
    – simon
    Commented Dec 1, 2012 at 23:19
5

You need to loop through the results (as TheSmose mentioned)

while ($request_list_row = $request_list_result->fetch_array()) {
    echo $request_list['user_fullname'];
}

AND you need to send the resulting array $request_list to the template rather than $request_list_row.

Change this

'request_list' => $request_list_row //right here

to this

'request_list' => $request_list //right here

If you want more than just user_fullname in your template (and you don't have PHP >= 5.3 required for mysqli_result::fetch_all), then you will need to build up your own array inside the loop.

I don't know what your template code expects, but you could try

while ($request_list_row = $request_list_result->fetch_array()) {
    echo $request_list[] = $request_list_row;
}
1

You need to loop through the results. As it stands, you're only fetching the first row.

$uid = get_uid();
$sql = "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='{$uid}'";

$request_list_result = $mysqli->query($sql);

while ($request_list_row = $request_list_result->fetch_array()) {
    echo $request_list['user_fullname'];
}
1
  • Hmm. I see your point, tried It but it does not work. Please check my last edit, and se if that could have influence on your answer :-)
    – simon
    Commented Dec 1, 2012 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.