Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.