|
-
Jan 25th, 2007, 05:10 PM
#1
Thread Starter
WiggleWiggle
Check DB if True
ok, so i have a members_friends table. like this:
Code:
member_id friend_id
------------------------
1 | 4
2 | 4
3 | 4
This meaning, That member '4' is friends to 1, 2, 3.
member_id friend_id
------------------------
4 | 1
4 | 2
4 | 3
This meaning that Members 1, 2, 3 are friends of 4.
What i need to do is look through the database to see if member '4' has friend '1' as a friend.
How do i do this?
My usual boring signature: Something
-
Jan 25th, 2007, 06:30 PM
#2
Hyperactive Member
-
Jan 26th, 2007, 02:16 AM
#3
Re: Check DB if True
As long as you know your MySQL basics (or even if you don't, I guess), I would suggest this instead. I always found user-made tutorials for MySQL much easier to comprehend and read through, as long as they were well written and not made by people who say "u" -- or people who think they know what they're talking about, but really don't.
In fact, I was going to make a new topic illustrating my MySQL problem because I couldn't make sense of the official documentation -- so I Googled quickly instead to save myself from having to post and wait for a reply, and the link above was the link to the solution of my problem.
But anyway, yeah, joins are what you're looking for.
-
Jan 26th, 2007, 09:19 AM
#4
Thread Starter
WiggleWiggle
Re: Check DB if True
Thanks so much kows! I am horrible with joins, i dont know why... I just dont understand them, but that tutorial helped.
THanks.
My usual boring signature: Something
-
Jan 27th, 2007, 12:50 AM
#5
Thread Starter
WiggleWiggle
Re: Check DB if True
would this be correct?:
Code:
SELECT *
FROM `members_friends`
WHERE member_friends.member_id='".$_GET['member_id']."'
AND member_friends.friend_id='".$_SESSION['member_id']."'
My usual boring signature: Something
-
Jan 27th, 2007, 01:20 AM
#6
Thread Starter
WiggleWiggle
Re: Check DB if True
i dont think i need inner join if it is the on the same table do i?
My usual boring signature: Something
-
Jan 27th, 2007, 01:47 PM
#7
Re: Check DB if True
why don't you just try and see if it did what you wanted or not?
-
Jan 27th, 2007, 01:48 PM
#8
Thread Starter
WiggleWiggle
Re: Check DB if True
 Originally Posted by kows
why don't you just try and see if it did what you wanted or not?
i have. i have changed it like 6 diffrent times and it still doesnt work.
My usual boring signature: Something
-
Jan 27th, 2007, 08:12 PM
#9
Re: Check DB if True
The SQL you need is quite simple. I suggest you learn it:
Code:
SELECT
friend_id
FROM
member_friends mf
WHERE
mf.member_id=<memberID> AND
mf.friend_if=<friendID>;
There is no need to use a join here because you are not sourcing data from another relation and you should certainly not join the table to itself.
Ensure that you escape any variables inserted into the query from outside. In actual fact you should use parametrized queries.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|