sciencebear Posted March 20, 2009 Share Posted March 20, 2009 I am currently building a messaging system for my website. I already have a user system. In the user MySQL table, there is both a user id and a user name. However, the messages table is separate, and only stores the senders' and recipients' user ids, but in the inbox, I want to show the user names. Right now, my code looks like this... $result = mysql_query("SELECT * FROM pmessages WHERE recipid='$userid'") or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo "No messages!"; } while($row = mysql_fetch_array($result)) { echo $row['senderid']; } How can I show the user name in the different table where the user id is the same as the sender id? Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/ Share on other sites More sharing options...
Maq Posted March 20, 2009 Share Posted March 20, 2009 You need to do a join, read more here. Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/#findComment-789493 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Share Posted March 20, 2009 can u show how ur table is structured? Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/#findComment-789498 Share on other sites More sharing options...
sciencebear Posted March 23, 2009 Author Share Posted March 23, 2009 I don't see how I could use a join. My db is like this: Table:pmessages -recipid -senderid -subject -message Table:user -userid -username And of course the user table has other fields like password and email, but I'm only using userid and username in this query. So instead of echoing senderid, I want it to echo the username where the userid is the same as the senderid. Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/#findComment-791720 Share on other sites More sharing options...
Maq Posted March 23, 2009 Share Posted March 23, 2009 I'm not sure what you should join on, maybe userid and recipid? But anyway, like I said, you have to use a join if you're using two different tables. Try: SELECT * FROM users u LEFT JOIN pmessages p ON u.userid = p.recipid WHERE p.recipid = '$userid'; Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/#findComment-791725 Share on other sites More sharing options...
sciencebear Posted March 23, 2009 Author Share Posted March 23, 2009 I'm not sure what you should join on, maybe userid and recipid? But anyway, like I said, you have to use a join if you're using two different tables. Try: SELECT * FROM users u LEFT JOIN pmessages p ON u.userid = p.recipid WHERE p.recipid = '$userid'; But that would take the recipient's row from the user table... so I should do that except with senderid? Does that sound right? Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/#findComment-791730 Share on other sites More sharing options...
Maq Posted March 23, 2009 Share Posted March 23, 2009 You don't have a recipid in the users table... Not from what your table structure tells me. If you just want username use this: $result = mysql_query("SELECT u.userid FROM users u LEFT JOIN pmessages p ON u.userid = p.recipid WHERE p.recipid = '$userid'") or die(mysql_error()); if (mysql_num_rows($result) > 0) { echo $row['username'] } else { echo "No Messages!"; } Quote Link to comment https://forums.phpfreaks.com/topic/150331-solved-messaging-system-displaying-user-names/#findComment-791732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.