Jump to content

[SOLVED] Joins?


NathanLedet

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.