php mysqli只返回一行

时间:2022-12-05 14:18:07

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

此代码仅返回一行,但应返回2行。我已经尝试过php myadmin中的sql,它完全返回了2行。我在这做错了什么?

$request_list_result = $mysqli->query("SELECT buddy_requester_id, buddy_reciepient_id, user_id, user_fullname FROM sb_buddiesJOIN sb_users ON buddy_requester_id=user_idWHERE buddy_status='0' AND buddy_reciepient_id='". get_uid() ."'");$request_list_row = $request_list_result->fetch_array();echo $request_list['user_fullname'];

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

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

$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 个解决方案

#1


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

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

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.

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

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

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

#2


 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

#3


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


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

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

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.

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

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

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

#2


 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

#3


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