Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2006, 10:44 PM   PM User | #1
shanparker
New Coder

 
Join Date: Sep 2004
Posts: 54
Thanks: 2
Thanked 0 Times in 0 Posts
shanparker is an unknown quantity at this point
Two columns, two values

I have this query semi finished. I have two tables. I need to query the first table for only entries where the "user" field is valid because it's a user in the "friends" table. I have that working. The third thing it needs to do is make sure that the user who is logged in is in friends table column.

Confusing.

The site I'm working on is a Myspace type site - so two entries go into the friends table when a friend link is created. An association each way for the friend. Friend 1 is friends with Friend 2, and a second one to show that Friend 2 is friends with Friend 1. The query I have written so far checks that they are friends - but it returns two results instead of one from the sticky_notes table because it's reading the two associations.

The query so far is:

PHP Code:
SELECT FROM sticky_notes,friends WHERE friends.friend sticky_notes.user LIMIT 5 
Now all I need to do is to get it to check for the third value. That the user (in the friends table) is the user who is logged in (which is the value $session->username).

I assume I'm on the right path here.
shanparker is offline   Reply With Quote
Old 06-19-2006, 11:06 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
You shouldn't assume that if user 1 is a friend of user 2 it automatically makes user 2 a friend of user 1.

You should be adding a one way association in the table. (that is for the logged in user only tag the person they identify as a friend and not also doing the reverse).

From there if you want to see if user 1 is logged in and show all their friends, then you should be checking a cookie to see if the user is logged in.

then just do a look up in the table for all friends of that user.
guelphdad is offline   Reply With Quote
Old 06-19-2006, 11:30 PM   PM User | #3
lavinpj1
Regular Coder

 
Join Date: Sep 2005
Posts: 394
Thanks: 1
Thanked 0 Times in 0 Posts
lavinpj1 is an unknown quantity at this point
You need to look at JOIN sql statements here.

~Phil~
lavinpj1 is offline   Reply With Quote
Old 06-20-2006, 12:43 AM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
The query above is a join, it is just joined incorrectly. the = has to have the column from one table joined to the column on another but I believe it is joined on the particular userid.

Code:
select 
foo, 
bar 
from tableA, tableB 
where 
tableA.column1=tableB.column7
is still a join for instance, it is just in list join syntax. It is much easier to make errors in such format as seen above.

Better to use:

Code:
select 
foo, 
bar 
from tableA
inner join tableB
on tableA.column1=tableB.column7
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:05 AM.


Advertisement
Log in to turn off these ads.