homepage Welcome to WebmasterWorld Guest from 54.227.67.175
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Relate two tables (user id and username)
matthewamzn




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

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)

<?php
$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/>";
}
?>

 

cameraman




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

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

matthewamzn




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

Thanks, works great!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved