Skip to main content
Participant
April 30, 2011
Question

help displaying a pic

  • April 30, 2011
  • 1 reply
  • 487 views

Hi Guys

Can anybody please help me with the following?

I have some code already in place which allows a logged in user to post a message. This message is then displayed on the page and also sent to a messgages table in a database along with the userid of the logged in user.

Currently, the profile picture of the logged in user is also displayed using:

<?php
//to display image from source
$dir = "profpics";

$sql = "SELECT prof_pic FROM users WHERE username = '{$_SESSION['MM_Username']}'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0) die("Username not found in database.");

$row = mysql_fetch_array($res);
echo "<img src='$dir/{$row['prof_pic']}' width='38' height='38'><br>";
    ?>

Is there anyway the "SELECT prof_pic FROM users WHERE username = '{$_SESSION['MM_Username']}'"; part can be edited to display the profile pic of the person who left the message?

I would imagine it would be something along the lines of:

"SELECT prof_pic FROM users WHERE message.id = userid?

This topic has been closed for replies.

1 reply

Community Expert
April 30, 2011

I don't think 2nd query is necessary.  You should just need to join the members table to this query so that you can grab certain profile elements when you return the post query.  The thing is, you can't use the session because that will only return the picture of the user that is logged in.  You need to return the user id from the post table when the post was recorded.

Participant
May 1, 2011

that makes sense, but the thing is, im not sure how to join the tables. Can you help?

Community Expert
May 1, 2011

Since I don't know anything about your table structure, keep in mind these names are representative and you will need to replace them with your table names.

SELECT * FROM post_table LEFT JOIN profile_table ON profile_table.profile_id = post_table.profile_id WHERE post_id = $_GET['id']

Basically, you select everything (can be more specific if need be) from the post table which includes the post identifier, post message and poster id.  That poster id field links it to the profile table field.  Then you use the mysql_fetch_assoc or mysql_fetch_array to pull out the image path as you would on the user profile page.  The WHERE clause basically just makes sure you have the right topic, I used the $_GET method but it is possible this is a $_POST method and depends on the setup of your script.  If it is threaded like a forum you may also have an ORDER BY clause at the end, but I'm just trying to keep it as simple as possible with the information provided.