NathanLedet Posted July 13, 2008 Share Posted July 13, 2008 I guess I need to learn how to use Joins for this little project I'm working on. An Addressbook with e-mail addresses affiliated with each user. Table Setup: Table 1 - addressbook userID, emailID, name, email Table 2 - users userID, username, password, email When the user logs into the admin area and they go to their addressbook, it pulls up a list of names and e-mail addresses affiliated with their user ID. They would need to be joined with the userID. in the table 'users', that number is the primary key which auto-increments. Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 13, 2008 Share Posted July 13, 2008 THIS PAGE has lots of good info about Joins in mySQL. Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted July 13, 2008 Author Share Posted July 13, 2008 I am making progress, but i'm not doing something right. Inside addressbook, I've given two entries the userID of "4", which relates to the user inside "users" with the userID of 4. code: $query = "SELECT users.userID, addressbook.userID FROM users, addressbook WHERE addressbook.userID = 4 ORDER BY name ASC"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo $row['name']; } This returns two blank spaces where the name is supposed to appear, but they don't show up. Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 13, 2008 Share Posted July 13, 2008 If you write it slightly differently, its easier to read... $query = "SELECT users.userID, addressbook.userID, addressbook.name"; $query .= " FROM users, addressbook"; $query .= " WHERE addressbook.userID = 4"; $query .= " ORDER BY addressbook.name ASC"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo $row['name']; } Try that and see if it works. EDIT: Still think a join is needed in there somewhere. I aint too hot on Joins myself however! Quote Link to comment Share on other sites More sharing options...
NathanLedet Posted July 13, 2008 Author Share Posted July 13, 2008 Thanks! I left out addressbook.name I guess I assumed that selecting the ID would select the name....but that didn't work out. It makes sense now and it works! *solved* Quote Link to comment 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.