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
https://forums.phpfreaks.com/topic/114553-solved-joins/
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
https://forums.phpfreaks.com/topic/114553-solved-joins/#findComment-589071
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
https://forums.phpfreaks.com/topic/114553-solved-joins/#findComment-589076
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.