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

  • SEARCH
  • Home
  • 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 8987253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:44:40+00:00 2026-06-15T21:44:40+00:00

I need some help writing a mysql statement in PHP. I have the same

  • 0

I need some help writing a mysql statement in PHP. I have the same foreign key referenced twice in the same table and I need to reference both instances with a SELECT statement. I.E. I have a table called messages, and then two columns labeled sender and receiver. Both columns link to the table users. I need to pull the info from sender-users and receiver-users in the same query. I’ve seen other answers on StackOverflow, but I don’t seem to be doing it correctly :/

Here is my current mysqli call in PHP with only grabbing the sender-users link:

require_once('includes/connection.inc.php');

$db = dbConnect();

$stmt = $db->stmt_init();

$stmt = $db->prepare("
SELECT message.timestamp, message_type.type, 
message.sender, message.receiver, message.sender_language, 
language.translate_code, message.subject_translate, message.body_translate,
message.subject, message.body, users.image, users.externalid, users.fname,
users.lname FROM message 
INNER JOIN users ON message.sender=users.id 
INNER JOIN language ON message.sender_language=language.id
INNER JOIN message_type ON message.type=message_type.id 
WHERE (message.receiver=? AND message.archived='n') 
ORDER BY message.timestamp DESC 
LIMIT 0,10 ");

$stmt->bind_param('i', $session_id);

$stmt->execute();

$stmt->bind_result($row['date'], $row['message_type'], $row['sender'],
$row['receiver'], $row['sender_language'], $row['sender_language_code'],
$row['subject'], $row['body'], $row['subject_original'], 
$row['body_original'], $row['image'], $row['user_name'], $row['fname'],
$row['lname'] );

$record_count = 0;

while($stmt->fetch()){...

This code works fine, but I need to add

"INNER JOIN users ON message.receiver=users.id"

correctly so I can include “users.image, users.externalid, users.fname, users.lname” with respect to message.receiver and not just message.sender

I was rewriting my code with reference to this question/answer but then got stuck: ForeignKey Referencing Same Table

  • 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-15T21:44:41+00:00Added an answer on June 15, 2026 at 9:44 pm

    Try this strategy using alias:

    SELECT msg.id, sender.id, receiver.id
    FROM message msg
    INNER JOIN users sender ON msg.sender=sender.id 
    INNER JOIN users receiver ON msg.receiver=receiver.id 
    WHERE [PUT_THE_WHERE_CONDITIONS_HERE]
    ORDER BY message.timestamp DESC 
    LIMIT 0,10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help writing a SQL statement. I have two tables in a
I need some help writing a regex. I have the following strings, xxx.yyy.wwwwwaaa_IN_123 xxx.rrrttttt_IN_12355
I need some help in writing a batch file. I have a path stored
I need help getting started with some PHP coding. I am writing an app
I need some help writing a MySQL trigger. what i want to do is
I need some help writing an http client. The trouble comes when I try
I'm new to web programming, so I need some help. I am writing a
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

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.