If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Need help with a simle SQL statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-26-08, 16:49
juanc juanc is offline
Registered User
 
Join Date: Dec 2005
Posts: 8
Need help with a simle SQL statement

Hi I'm trying to start a dating site and need help with an SQL statement. Basically all the members would be shown when searched for except for those who have blocked the member that is visiting and searching. I'll know who the member that's visiting is by capturing is username with php $_SESSION['username'] and also his IP address.

These are the columns in the tables I have...

members

id,
username,
photo,
ip

blocked

id,
name_of_banned,
ip_of_banned,
banned_by
member_id_of_banned_by

so basically select all from members except for the members appearing in the
"banned_by" column of the blocked table that also have named_of_banned = to the current $_SESSION['username'] or the ip_of_banned = to the current users IP (I'll use $_SERVER['REMOTE_ADDR'])

Please note I want to be able to check by both $_SESSION['username'] and $_SERVER['REMOTE_ADDR'] since perhaps the user that's banned may be browsing whilst not logged in.

many thanks
juanc is online now Report Post Edit/Delete Message
Reply With Quote
  #2 (permalink)  
Old 04-26-08, 17:21
Jackboy Jackboy is offline
Registered User
 
Join Date: Apr 2008
Location: West Sussex
Posts: 19
Quote:
Originally Posted by juanc
Hi I'm trying to start a dating site and need help with an SQL statement. Basically all the members would be shown when searched for except for those who have blocked the member that is visiting and searching. I'll know who the member that's visiting is by capturing is username with php $_SESSION['username'] and also his IP address.

These are the columns in the tables I have...

members

id,
username,
photo,
ip

blocked

id,
name_of_banned,
ip_of_banned,
banned_by
member_id_of_banned_by

so basically select all from members except for the members appearing in the
"banned_by" column of the blocked table that also have named_of_banned = to the current $_SESSION['username'] or the ip_of_banned = to the current users IP (I'll use $_SERVER['REMOTE_ADDR'])

Please note I want to be able to check by both $_SESSION['username'] and $_SERVER['REMOTE_ADDR'] since perhaps the user that's banned may be browsing whilst not logged in.

many thanks
juanc is online now Report Post Edit/Delete Message
PHP Code:
$getusers mysql_query("SELECT * FROM `blocked`");
while (
$gotusers mysql_fetch_array($getusers)){
$prousers mysql_query("SELECT * FROM `members` WHERE `username` != '$gotusers[username]'");
while (
$allusers mysql_fetch_array($prousers)){
echo (
"$allusers[username]<br>");
}

sorry if i have mucked up the code, i havn't tested it... So yeh :P But hope it works. Any problems just reply.
Reply With Quote
  #3 (permalink)  
Old 04-26-08, 17:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
is that a query inside a loop? does the inner query return all the members except one? and the inner query does this as many times as there are rows in the blocked table?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 04-26-08, 19:16
Jackboy Jackboy is offline
Registered User
 
Join Date: Apr 2008
Location: West Sussex
Posts: 19
ahh yes, u have made me think. I have juffed it up.

Im off to bed now (GMT) but if nobody has sorted it by morning i will happily fix.

Thanks for pointing that out though mate.
Reply With Quote
  #5 (permalink)  
Old 04-27-08, 04:38
juanc juanc is offline
Registered User
 
Join Date: Dec 2005
Posts: 8
Hi there since I've posted this I've found the answer so I thought I would post here so anyone else may benefit.....I needed a NOT IN subquery

SELECT username
FROM members
WHERE username NOT
IN (

SELECT banned_by
FROM blocked
JOIN members ON members.username = blocked.name_of_banned
WHERE blocked.name_of_banned = '$_SESSION[username]' OR blocked.ip_of_banned = '$_SERVER[REMOTE_ADDR]'
)
LIMIT 0 , 30
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On