MySQLi 查询只返回一行

2021-12-25 00:00:00 return row php mysqli

这段代码只返回一行,但应该返回两行.我在 phpMyAdmin 中尝试了 SQL,它完美地返回了 2 行.我在这里做错了什么?

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

顺便说一下,上面的代码是通过以下脚本获取到 profile.php 的过程:

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);

推荐答案

你需要遍历结果(正如 TheSmose 提到的)

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 您需要将结果数组 $request_list 发送到模板而不是 $request_list_row.

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

改变这个

'request_list' => $request_list_row //right here

至此

'request_list' => $request_list //right here

如果您想要的不仅仅是模板中的 user_fullname(并且您没有 mysqli_result::fetch_all 所需的 PHP >= 5.3),那么您将需要在循环内建立你自己的数组.

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;
}

相关文章