Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>2 things:</p> <pre><code>if ($row['username_id'] == 1) </code></pre> <p>you should put that in your sql:</p> <pre><code>$result = mysql_query("SELECT username, friendname FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.username_id where tbusers.id = ".$yourVariable); </code></pre> <p>In your query, user and friend are linked, and can only be equal.</p> <p>Now, this:</p> <pre><code>$count = $row['friendname'];//friendname is in friends if ($row['id'] == $count)//id is in users </code></pre> <p>is equal to </p> <pre><code>if ( $row['id'] == $row['friendname'] ) </code></pre> <p>this sounds plain wrong. You compare a numerical id with a name. Moreover, your sql query already retrives all friends from users. In the version I showed here, it retrieves only friends of the user you are interested in.</p> <p>finally, you print (echo) the name of the user, not of the friend. In my opinion the following code will do what you want:</p> <pre><code>$result = mysql_query("SELECT friendname FROM tbfriends WHERE username_id = ".$yourUserVariable); while($row = mysql_fetch_array($result)) { echo $row['friendname']; // or better: echo $row['friendname'], '&lt;br&gt;'; } </code></pre> <p>edit: after comment...</p> <p>so <code>if ( $row['id'] == $row['friendname'] )</code> means the user is his own friend. can that happen?</p> <p>this code shall print what you want, friend names.</p> <pre><code>/* $result = mysql_query(" SELECT username as friend_name, friendname as friend_id, username_id as user_id FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.username_id WHERE tbusers.id = ".$yourVariable); </code></pre> <p>*/</p> <pre><code>$result = mysql_query(" SELECT username as friend_name, friendname as friend_id, username_id as user_id FROM tbusers INNER JOIN tbfriends ON tbusers.id = tbfriends.friendname WHERE tbfriends.username_id = ".$yourVariable); while($row = mysql_fetch_array($result)) { echo $row['friend_name']; // or better: echo $row['friend_name'], '&lt;br&gt;'; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload