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.

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";

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!

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.