在页面上获取多个mysql结果

时间:2014-01-19 00:26:12

标签: php mysql list

你好,所以我在这里遇到一些麻烦我想要做的是使用下面的代码在同一页面上显示所有用户的朋友请求,但发生的事情是页面只显示一个请求时间然后页面必须刷新或重新加载,以便剩下的请求一个接一个地显示,而不是在页面下面列出的任何帮助都会很好,因为我正在努力学习谢谢

<?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

    }



?>

3 个答案:

答案 0 :(得分:0)

这是因为你只打印出一个。你永远不会遍历包含所有朋友请求的数组 - 你回显数组中的第一个。如果你拒绝或接受那个,它将显示下一个,依此类推。

请参阅Populate PHP Array from While Loop

答案 1 :(得分:0)

函数mysql_fetch_array()一次只提取一个结果行。你想把它包装在while循环中,如下所示:

while ($get_request_details = mysql_fetch_array($check_request))
{
  // do repetitive processing 
}

答案 2 :(得分:0)

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

在SQL中使用LIMIT是一个很好的做法