简体   繁体   中英

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

The function mysql_fetch_array() only fetches one result row at a time. You want to wrap this in a while loop like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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