Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Relate two tables (user id and username)



5:15 am on Sep 18, 2008 (gmt 0)

10+ Year Member

I have two tables. The first has usernames and id's. The second has messages and user id's. How can I get the username beside each message?

(user_id and id have the same values)

$user_query = "SELECT id, username FROM v_users";
$user_result = mysql_query($user_query);

$message_query = "SELECT user_id, message, time FROM v_messages ORDER BY time ASC LIMIT 20";
$message_result = mysql_query($message_query);

while ($message = mysql_fetch_array($message_result)) {
echo $message[1] . "<br/>";


6:22 am on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Nuke the first query (unless you need it for something else). Change the second query to:
SELECT user_id, message, time,username FROM v_messages, v_users WHERE (id=user_id) ORDER BY time ASC LIMIT 20


6:53 am on Sep 18, 2008 (gmt 0)

10+ Year Member

Thanks, works great!

Featured Threads

Hot Threads This Week

Hot Threads This Month