Jump to content

[SOLVED] my Join query is displaying 2 of each result


NathanLedet

Recommended Posts

This code works, but it displays two of each database entry....I'm new with Joins, so i must be doing something wrong....clearly

 

<table width="500px" border="1px">
<tr>
    	<td width="25%">Name</td>
        <td width="50%">E-mail Address</td>
        <td width="15%">Options</td>
    </tr>
</table>

<div style="width:500px; overflow:auto;">
<table width="500px" border="0" id="addressbook">
    	<?php
	$query = "SELECT * FROM users, addressbook WHERE addressbook.userID = 2";
	$result = mysql_query($query);
      while($row = mysql_fetch_array($result, MYSQL_ASSOC)){  
       echo "<tr>\n";
       echo "<td width=\"25%\" class=\"tbldata\">" . $row['name'] . "</td>\n";
       echo "<td width=\"60%\" class=\"tbldata\">" . $row['email'] . "</td>\n";
       echo "<td width=\"15%\" class=\"tbldata\">edit/delete</td>\n";            
       echo "</tr>\n";
	}        
        ?>
     </table>
</div>

 

Oh and a little about my table structure:

 

table: users

userID

username

password

email

 

table: addressbook

userID

emailID

name

email

 

The table 'users' is a list of people allowed to access the site.  the table 'addressbook' is each 'users' personal e-mail addressbook.  I am trying to join them together through the userID.  the userID inside 'users' is unique and the userID inside 'addressbook' belongs to whichever 'user' added them to the database.

Link to comment
Share on other sites

You are not specifying the join condition

$query = "SELECT * FROM users, addressbook 
            WHERE users.userID = addressbook.userID
            AND addressbook.userID = 2";

 

or, better

 

$query = "SELECT * FROM users
             JOIN addressbook ON users.userID = addressbook.userID
            WHERE addressbook.userID = 2";

Link to comment
Share on other sites

Awesome those work!!! THANKS

 

I even managed to re-work it so that it uses Session data :D

$query = "SELECT * FROM users
             JOIN addressbook ON users.userID = addressbook.userID
            WHERE users.username = '" . $_SESSION['MM_Username'] . "'";

 

I appreciate your help!

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.