Jump to content

[SOLVED] query to friend table


jakebur01

Recommended Posts

Could someone please show me a better way to do this so I will not have to make 2 queries out of it?

 

<?PHP

$sql = mysql_query("SELECT UserId1 FROM life_approval WHERE `UserId2`='$_SESSION[valid_user]' AND `Approve` = 'Approved'");
					$num = mysql_num_rows($sql);
					if ($num > 0) {
                          // we are good, this user is a friend
					}

               elseif {
			   $sql = mysql_query("SELECT UserId2 FROM life_approval WHERE `UserId1`='$_SESSION[valid_user]' AND `Approve` = 'Approved'");
					$num = mysql_num_rows($sql);
			   if ($num > 0) {
                        // we are good, this user is a friend
				}
		   }
		   
	else {
		//do nothing, this is not a friend
	}

?>

Link to comment
https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/
Share on other sites

$sql = mysql_query("SELECT userId1, userId2 FROM life_approval WHERE (`userId1` = '{$_SESSION['valid_user']}' OR `userId2` =  '{$_SESSION['valid_user']}') AND `approve` = 'Approved'");

 

This is really a MySQL question and should be posted in that forum. But the above should get you what you want in 1 statement.

 

you mean this i guess or not.

 

got beat lol

<?php
$sql = mysql_query("SELECT UserId1,UserId2 FROM life_approval WHERE `UserId2`='{$_SESSION['valid_user']}' OR `UserId2`='{$_SESSION['valid_user']}' AND `Approve` = 'Approved'");
                  $num = mysql_num_rows($sql);
                  if ($num > 0) {
                          // we are good, this user is a friend
                  }
?>
                  

 

Sorry...

 

I am trying to check and see if $_SESSION[valid_user] and $user_profile are friends

 

correction:

WHERE `UserId2`='$_SESSION[valid_user]' AND `UserId1`='$user_profile' AND `Approve` = 'Approved'

 

 

and

WHERE `UserId1`='$_SESSION[valid_user]' AND `UserId2`='$user_profile' AND `Approve` = 'Approved'

 

 

So, will this check both ways?

$sql = mysql_query("SELECT userId1, userId2 FROM life_approval WHERE (`userId1` = '{$_SESSION['valid_user']}' OR `userId2` =  '{$_SESSION['valid_user']}') AND `approve` = 'Approved'");

 

I should have posted this in mysql. I will take care to do that next time.

 

 

Should it be similar to this?


$sql = mysql_query("SELECT userId1, userId2 FROM life_approval WHERE (`userId1` = '{$_SESSION['valid_user']}' AND `UserId2`='$user_profile' OR `userId2` =  '{$_SESSION['valid_user']}')AND `UserId1`='$user_profile' AND `approve` = 'Approved'");

 

 

It worked with this:

 $sql = mysql_query("SELECT UserId1, UserId2 FROM life_approval WHERE (`UserId1` = '{$_SESSION['valid_user']}' AND `UserId2`='$q' OR `UserId2` =  '{$_SESSION['valid_user']}' AND `UserId1`='$q') AND `Approved` = 'Approved'");

 

That is incredible. Now, I can really get some stuff done.

 

Thank you all so much, I have been wanting to figure this out.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.