0

How can I make it so that the code

$requests = mysql_query("SELECT * FROM friend_reqs WHERE user_to={$_SESSION['user']['user_id']}");<br />
$users = mysql_query("SELECT user_name, user_pic FROM users WHERE user_id={?}");

>echo "`<h2>Pending Requests</h2>`";

>echo "`<center><table class='squish'>";
  if(mysql_num_rows($requests) >= 1){
    while($un = mysql_fetch_assoc($users)){
        echo "<tr><form action='' method='post'>";
            echo "<img src='https://www.mastergamersocial.com/user_pics/".$un['user_pic']."' alt='".$un['user_name']."' width='50px' height='50px' />".$un['user_name'];
            echo "<input class='a-button' type='submit' value='Accept' name='yes' />";
            echo "<input class='a-button' type='submit' value='Decline' name='no' />";
        echo "</form><br /></tr>";
    }
  } else {
    echo "<tr><h3>You have no friend requests.</h3></tr>";
  }
echo "</table></center>";

Sorry for the sloppy code. I need to get it so that it pulls the user_id and name from the user who sent the request. But I can't figure out how to get it unless i call the query later. But I need the query to be called when it is.

1 Answers1

2

Summing from your comments, if you need the first value of user_from from $requests, you could simply fetch your first record after the query and insert it in the second one like:

$requests = mysql_query("SELECT * FROM friend_reqs WHERE user_to={$_SESSION['user']['user_id']}");
$row = mysql_fetch_assoc($requests);
$from_user = $row['user_from'];
$users = mysql_query("SELECT user_name, user_pic FROM users WHERE user_id={$from_user}");

Now if your $requests is returning multiple records and you want to execute $users with each user_from value. Then you'll have to run the second query in a loop.

You can have one query being fetched multiple times by different variables, if that's what you were unclear about.

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
AyB
  • 11,609
  • 4
  • 32
  • 47