Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck If Some User ID's Are Friends With The Logged In User In A Single Query
    primarykey
    data
    text
    <p>I have a simple friends system on my site. Now I'm trying to create a multi-user group messaging system but only between friends. The "To" values of my messaging system are comma values, I want to check if they all are friend with the person sending the message.</p> <p>For example, I can get all of a user's friends by the following query:</p> <pre><code>SELECT relation_setter user_id FROM users_relationships WHERE relation_getter = {$logged_in_user} AND active = 1 UNION SELECT relation_getter user_id FROM users_relationships WHERE relation_setter = {$logged_in_user} AND active = 1 </code></pre> <p>and I have <code>natasha, reeta</code> as a $_POST['to'] value from my message form which I then convert to user ids and get something like <code>126152, 112238</code></p> <p>Now how do I check if these ids are both friends with the logged in user, in a single query. I don't want to run my is_friend function in a loop, which check if a single userid is friends with the logged in user.</p> <p><strong>Relationships Schema:</strong></p> <p><strong>users_relationships</strong></p> <pre><code>id PK relation_setter FK user_id relation_getter FK user_id active smallint(1) </code></pre> <p>The <code>relation_setter</code> is the one who sent the friend request. To get all my friends I get all the IDs where my ID is either the <code>relation_setter</code> or <code>relation_getter</code>.</p> <p><strong>users</strong></p> <pre><code>user_id PK name username password etc etc... </code></pre>
    singulars
    1. This table or related slice is empty.
    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