Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if current user id is not equal to database entry?
    primarykey
    data
    text
    <p>I have a friends page which pulls rows from a database, I only want to display the current users friends but it only returns the current user from the query.</p> <p>Database Table: </p> <p><img src="https://i.stack.imgur.com/MrZFa.png" alt="enter image description here"></p> <p>Current Code: </p> <pre><code>function getFriends($user_id, $sqli_con) { $user_id = mysqli_escape_string($sqli_con, strip_tags($user_id)); $results = $sqli_con-&gt;query("SELECT * FROM friends WHERE receiver_id = '$user_id' AND accepted = '1' OR sender_id = '$user_id' AND accepted = '1'"); if($results-&gt;num_rows &gt; 0) { while($row = $results-&gt;fetch_array(MYSQLI_ASSOC)) { if($row['receiver_id'] === $user_id || $row['sender_id'] === $user_id) { } else { $username_stmt = $sqli_con-&gt;prepare("SELECT id, username FROM members WHERE id = {$row['receiver_id']} OR id = {$row['sender_id']}"); $username_stmt-&gt;execute(); $username_stmt-&gt;store_result(); $username_stmt-&gt;bind_result($id, $username); $username_stmt-&gt;fetch(); $username_stmt-&gt;close(); $results-&gt;free(); return " &lt;div id='friend'&gt; &lt;a href='profile.php?id=".$id."'&gt;&lt;img src='". getProfileImagePath($id, $sqli_con) . "' /&gt;&lt;/a&gt; &lt;a href='profile.php?id=".$id."'&gt;". $username . "&lt;/a&gt; &lt;/div&gt; "; } } } else { $results-&gt;free(); return "You don't have any friends yet! :( Why not search for some?"; } } </code></pre> <p>Currently no results are returned but if I take out the user id check it only returns the current user.</p> <p>EDIT: I got it working, solution:</p> <pre><code>function getFriends($user_id, $sqli_con) { $user_id = mysqli_escape_string($sqli_con, strip_tags($user_id)); $results = $sqli_con-&gt;query("SELECT * FROM friends WHERE (receiver_id = '$user_id' OR sender_id = '$user_id') AND accepted = '1'"); if($results-&gt;num_rows &gt; 0) { while($row = $results-&gt;fetch_array(MYSQLI_ASSOC)) { if($row['sender_id'] !== $user_id) { $username_stmt = $sqli_con-&gt;prepare("SELECT username FROM members WHERE id = {$row['sender_id']}"); $username_stmt-&gt;execute(); $username_stmt-&gt;store_result(); $username_stmt-&gt;bind_result($username); $username_stmt-&gt;fetch(); $username_stmt-&gt;close(); echo " &lt;div id='friend'&gt; &lt;a href='profile.php?id=".$row['sender_id']."'&gt;&lt;img src='". getProfileImagePath($row['sender_id'], $sqli_con) . "' /&gt;&lt;/a&gt; &lt;a href='profile.php?id=".$row['sender_id']."'&gt;". $username . "&lt;/a&gt; &lt;/div&gt; "; } if($row['receiver_id'] !== $user_id) { $username_stmt = $sqli_con-&gt;prepare("SELECT username FROM members WHERE id = {$row['receiver_id']}"); $username_stmt-&gt;execute(); $username_stmt-&gt;store_result(); $username_stmt-&gt;bind_result($username); $username_stmt-&gt;fetch(); $username_stmt-&gt;close(); echo " &lt;div id='friend'&gt; &lt;a href='profile.php?id=".$row['receiver_id']."'&gt;&lt;img src='". getProfileImagePath($row['receiver_id'], $sqli_con) . "' /&gt;&lt;/a&gt; &lt;a href='profile.php?id=".$row['receiver_id']."'&gt;". $username . "&lt;/a&gt; &lt;/div&gt; "; } } } else { $results-&gt;free(); return "You don't have any friends yet! :( Why not search for some?"; } } </code></pre> <p>Not pretty but it'll do. Thanks for the help guys.(Tested with multiple entries and it works)</p>
    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.
 

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