jakebur01 Posted March 8, 2009 Share Posted March 8, 2009 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/ Share on other sites More sharing options...
DjMikeS Posted March 8, 2009 Share Posted March 8, 2009 You could start by telling us what you are trying to do ? Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779784 Share on other sites More sharing options...
premiso Posted March 8, 2009 Share Posted March 8, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779786 Share on other sites More sharing options...
redarrow Posted March 8, 2009 Share Posted March 8, 2009 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779787 Share on other sites More sharing options...
jakebur01 Posted March 8, 2009 Author Share Posted March 8, 2009 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' Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779790 Share on other sites More sharing options...
jakebur01 Posted March 8, 2009 Author Share Posted March 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779797 Share on other sites More sharing options...
jakebur01 Posted March 8, 2009 Author Share Posted March 8, 2009 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'"); Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779806 Share on other sites More sharing options...
redarrow Posted March 8, 2009 Share Posted March 8, 2009 try it first then tell us. Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779812 Share on other sites More sharing options...
jakebur01 Posted March 8, 2009 Author Share Posted March 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148496-solved-query-to-friend-table/#findComment-779819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.