php query duplicating entries on echo?
<p>I'm having troubles with this php query as it doesn't seem to be working like I intended. It's grabbing more then 10 entries and seems to be duplicating some of them. I haven't fully understood JOINS or I'd be using them. Perhaps someone could explain how they work in detail? I've already tried reading up on them, but I just couldn't wrap my head around them.</p>

<p>What this does is it finds all of a users friends, then it finds all of a users fans, combines them into one variable ($name) which is where I'm guessing a majority of the problems are coming from. After that it gets the status updates from both of those groups and echoes the status updates.</p>

<p>Here's the code: </p>

<pre><code>&lt;?php
include("dbcon.php");
$user = "vacarsu";
$x = 0;
$result1 = mysql_query("SELECT * FROM friends WHERE username1='$user' OR username2='$user'") or die(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
if($row1['username1'] = $user) {
    $friend = $row1['username2'];
}
else {
    $friend = $row1['username1'];
}
$result2 = mysql_query("SELECT * FROM fans WHERE username='$user' AND fanned!='$friend'") or die(mysql_error());
while($row2 = mysql_fetch_array($result2)) {
    $fan = $row2['fanned'];
    if($friend != "") {
            $name = $friend;
        }
        else {
            $name = $fan;
        }
    $result3 = mysql_query("SELECT * FROM supdates WHERE username='$name' ORDER BY timestamp DESC LIMIT 10") or die(mysql_error());
    while($row3 = mysql_fetch_array($result3)) {
        if($row3['username'] = $friend) {
            $bordertype = '&lt;div align="left" id="profileborderstatusupdatefriend"&gt;&lt;center&gt;&lt;img id="avatarsmall" src="images/nopic.PNG" /&gt;&lt;/center&gt;&lt;/div&gt;';
        }
        else {
            $bordertype = '&lt;div align="left" id="profileborderstatusupdatefan"&gt;&lt;center&gt;&lt;img id="avatarsmall" src="images/nopic.PNG" /&gt;&lt;/center&gt;&lt;/div&gt;';
        }
        $statusupdate = $row3['content'];
        $x = $x + 1;
        echo
            '&lt;div class="status' . $x . '" id="status"&gt;
            &lt;tr&gt;&lt;td&gt;' .
                $bordertype
            . '&lt;/td&gt;&lt;/tr&gt;
            &lt;tr&gt;&lt;td&gt;
                &lt;div align="left" class="statusupdate' . $x . '" id="statusupdate"&gt;' .
                    $name . '&lt;br  /&gt;&amp;nbsp;&amp;nbsp;' . $statusupdate .
                    '&lt;span class="quickinteractbts" id="quickinteractbts"&gt;&lt;center&gt;&lt;a href="#"&gt;aji&lt;/a&gt; &lt;a href="#"&gt;kibits&lt;/a&gt;&lt;/center&gt;&lt;/span&gt;
                &lt;/div&gt;
                &lt;div id="statusupdateinteraction"&gt;&lt;span id="interact"&gt;give aji kibitz!&lt;/span&gt;&lt;span id="updatestatusstats"&gt;200 140&lt;/span&gt;
                &lt;/div&gt;
                &lt;div class="comments'. $x . '" id="comments"&gt;' .
                    $bordertype . '&lt;span id="comment"&gt;Fumbles40&lt;br /&gt;&amp;nbsp;&amp;nbsp;lol&lt;/span&gt;
                    &lt;div id="addcomment"&gt;&lt;br /&gt;&lt;center&gt;&lt;span class="addcommentspan"&gt;&lt;input value="comment..." type="text" class="addcommentinput" id="addcommentinput" maxlength="50" /&gt;&lt;/span&gt;&lt;/center&gt;
                    &lt;/div&gt;
                &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
            &lt;/td&gt;&lt;/tr&gt;
        &lt;/div&gt;';

    }
}
}

mysql_close($con);
?&gt;
</code></pre>
