简体   繁体   English

在页面上获取多个mysql结果

[英]Getting more than one mysql result on a page

Hello there so I am having a little trouble here what I am trying to do is use my code below to show all the users friend requests on the same page, but what is happening is the page is showing only one request at a time then the page has to be refreshed or reloaded for the remaining requests to show up one by one instead off all of them listed down the page any help would be great as I'm trying to learn thank you 您好,所以我在这里遇到了一些麻烦,我想要做的是使用下面的代码在同一页面上显示所有用户朋友的请求,但是正在发生的情况是该页面一次仅显示一个请求,然后必须刷新或重新加载页面,以使剩余的请求一个接一个地显示,而不是关闭页面下方列出的所有请求,因为我正在努力学习,谢谢您的帮助

<?php
include ('views/header.php');
require_once ('config/config.php');
include ('config/connection.php');


{

    global $user_name,$page_owner,$username;

    $user_name = trim(strip_tags($_SESSION["user_name"])); //This is the user who logged into the system or logged in session

    $page_owner = trim(strip_tags($_SESSION["user_name"])); // This is the owner of the page viewed
            $username = mysql_query("select * from request where friend ='".$user_name."'");
    $user_id = mysql_query("select user_id from users where user_id = 'user_id'");


    //This is the page that checks for Friend Request


 $check_request = mysql_query("select * from request where friend = '".$user_name."'"); //First Request receive, first to respond to


                    if(intval(mysql_num_rows($check_request))==0); //If there is a friend request for the logged in user then show it to the user otherwise do nothing




                            $get_request_details = mysql_fetch_array($check_request);


                            //Check friend who sent the request full info from the users table

                            $check_request_info = mysql_query("select * from `users` where `user_name` = '".mysql_real_escape_string($get_request_details["username"])."'");

                            //Get friend who sent the request full info from the users table

                            $get_request_info = mysql_fetch_array($check_request_info);


                            //Check logged in user full info from the users table

                            $check_logged_in_user_info = mysql_query("select * from `users` where `user_name` = '".$_SESSION['user_name']."'");

                            //Get logged in user full info from the users table

                            $get_logged_in_user_info = mysql_fetch_array($check_logged_in_user_info);

?>


new requests(<?php echo intval(mysql_num_rows($check_request)); ?>)







                           <div>Hello <?php echo strip_tags($get_logged_in_user_info["user_name"]);?><div>

                            <div style="font-family:Verdana, Geneva, sans-serif; font-size:11px; line-height:18px;" align="left">Here are your friend requests.</div>

                            <a href="userpro.php?id=<?php echo $get_request_info["user_id"]; ?>"><font style="color:blue;font-family:Verdana, Geneva, sans-serif; font-size:14px;"><?php echo strip_tags($get_request_info["user_name"]); ?> wants to be friends</font></a><div>


                            <div>



<div>

<a href="af.php?username=<?php echo  $get_request_info["user_name"];
?>"class="square">Accept</a>


                            <a href="df.php?username=<?php echo $get_request_info["user_name"]; ?>"class="square">Decline</a>


 <?php




                    }













    {

            //Unknown page realized

    }



?>

It's because you only print out one. 这是因为您只打印了一张。 You never loop through the array containing all friend requests - you echo the first one in the array. 您永远不会遍历包含所有好友请求的数组-您回显数组中的第一个请求。 If you decline or accept that one, it'll show the next one, and so forth. 如果您拒绝或接受其中一个,则会显示下一个,依此类推。

See Populate PHP Array from While Loop 请参见从While循环填充PHP数组

The function mysql_fetch_array() only fetches one result row at a time. 函数mysql_fetch_array()一次只获取一个结果行。 You want to wrap this in a while loop like so: 您希望将其包装在while循环中,如下所示:

while ($get_request_details = mysql_fetch_array($check_request))
{
  // do repetitive processing 
}
$user_name = mysql_real_escape_string($user_name);
$query ="SELECT user_id FROM users JOIN request ON users.user_id = request.user_id WHERE users.user_id = {$user_name}";
$request = mysql_query($query);
while ( $row = mysql_fetch_assoc($request)){
    $results[] = $row;
}
//debug
echo '<pre>';
print_r($results);

and it's a good practice to use LIMIT in your SQL 在SQL中使用LIMIT是一个好习惯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM