Millica Millica - 1 year ago 95
MySQL Question

Can't display avatar - PHP, MySQL

I've created a private message system and I'm trying to show the avatar for the person who receives the message, but instead of getting the avatar for the receiver, it's trying to get the image for the logged in user.

For the image source, avatars are stored in a folder called

. The layout of the folder is user/username/avatar. However, when implemented, the correct user folder is opened, but it searches for the avatar of the logged in user.

Also, one more thing to note. For some reason the avatar of the
(parent message) is shown, but not the receiver.

Here's my PHP.

$sql = "
INNER JOIN pm ON users.username = pm.sender
AND parent='x' AND rdelete='0'
OR (
AND sdelete='0' AND parent='x'
ORDER BY senttime DESC";
$query = mysqli_query($db, $sql);
$statusnumrows = mysqli_num_rows($query);

if($statusnumrows > 0){
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
//assigning variables
$avatar = $row["avatar"];
$sender = $row["sender"];
$receiver = $row["receiver"];

if($parent == "x" && $sender == $logged_in_user){
$profile_pic = '<img src="user/'.$receiver.'/'.$avatar.'" alt="'.$receiver.'">';
echo $profile_pic;

Answer Source

So, this PHP is supposed to yield the receiver's avatar?

Look at the SQL here:

INNER JOIN pm ON users.username = pm.sender 

Try instead:

INNER JOIN pm ON users.username = pm.receiver

(assuming of course the column is named "receiver" in your schema).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download