Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7782087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:25:56+00:00 2026-06-01T19:25:56+00:00

Using PHP and MySQL, I’m trying to get all the private chats involving the

  • 0

Using PHP and MySQL, I’m trying to get all the private chats involving the actual user.
For that I have a table called “rooms” (id, id_initiate_user, id_target_user) and another one called “users” (id_pseudo, avatar).
If the actual user is the initiating user of a chat room, I want to get the avatar and pseudo from the target user and if the actual user is the target user of the that room, I want to get the avatar and pseudo from the initiating user (whatever the case, I want the other member’s avatar and pseudo).

I guess I need a condition in my query then but I really don’t know how to manage it (googled all day long).

Here is my query (the logic is there but the syntax is obviously wrong:

$user_id=$_SESSION['user_id'];

$res = @mysql_query("
SELECT rooms.id, rooms.id_initiate_user, rooms.id_target_user, rooms.pv, users.id, users.pseudo, users.avatar 
FROM rooms, users 
WHERE 
(
    (rooms.id_initiate_user='$user_id' OR rooms.id_target_user='$user_id') 
    AND  
    (
        IF (rooms.id_initiate_user='$user_id')
        THEN rooms.id_target_user=users.id
        ELSE rooms.id_initiate_user=users.id;
        END IF;
    )
) ORDER BY rooms.id ASC");

I read plenty of questions around here regarding the “conditional where” but none of it solved my case.
Thank you for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:25:57+00:00Added an answer on June 1, 2026 at 7:25 pm

    Can’t you do something like this:

    WHERE 
    (
        (
            rooms.id_initiate_user='$user_id'
            AND rooms.id_target_user=users.id
        )
        OR
        (
            rooms.id_target_user='$user_id'
            AND rooms.id_initiate_user=users.id
        )
    ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP / MySQL all encoded up as UTF, we have recently had to
Using PHP and MySQL, I have a forum system I'm trying to build. What
using php and mysql I have two tables, a users table and a profiles
i am using php/mysql. i have a product table . in which most product
I have a web application that needs to be built using PHP/MySQL. The application
I have been using php/mysql for a while now, I want to sanitize all
So I'm using PHP/MySQL and I have a tbl_profile table with the following fields:
I am using php/mysql, and have a database table with image url. I would
I am trying to create some pages using PHP / MySQL. My database table
I am using PHP/MySQL to handle the image uploading. I want all images that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.