問題描述
這段代碼只返回一行,但應該返回兩行.我在 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;
}
這篇關于MySQLi 查詢只返回一行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!