N-Bomb(Nerd) Posted January 13, 2010 Share Posted January 13, 2010 Hi, Sorry for bad title.. didn't know what to call this. I have two tables set up in my mysql database: posts{poster_id, post, date} users{user_id, username, password, email} The poster_id and user_id is the same number.. just the user table holds all the user information and posts table holds all the post information, but keeps the posters username out of it and instead uses their id. I don't know how to take poster_id and match it with user_id in users table and return username. I tried reading about this and somewhere it says I had to do a JOIN. I tried to look at JOIN on http://www.tizag.com/mysqlTutorial/mysqljoins.php and now im confused more. can someone please explain how to do this for me.. i want to learn inside of someone just give me working code Link to comment https://forums.phpfreaks.com/topic/188380-finding-username/ Share on other sites More sharing options...
Andy-H Posted January 13, 2010 Share Posted January 13, 2010 $query = "SELECT u.username FROM users AS u JOIN posts AS p ON u.user_id = p.poster_id"; $result = mysql_query($query); $row = mysql_fetch_row($result); echo $row[0]; // username Link to comment https://forums.phpfreaks.com/topic/188380-finding-username/#findComment-994496 Share on other sites More sharing options...
N-Bomb(Nerd) Posted January 13, 2010 Author Share Posted January 13, 2010 thanks for working code.. im having some trouble changing it so i can use it it.. im trying to make script to display posts and display in this format Post Username: posternamehere Post Date: postdatehere Post Content: postcontenthere i cant figure out how to change what you've provided me with to select all those but still get the poster username from poster_id all in the same query.. is that even possible or do I have to do two queries? Link to comment https://forums.phpfreaks.com/topic/188380-finding-username/#findComment-994502 Share on other sites More sharing options...
Andy-H Posted January 13, 2010 Share Posted January 13, 2010 $query = "SELECT u.username, p.`date`, p.post FROM users AS u JOIN posts AS p ON u.user_id = p.poster_id"; $result = mysql_query($query); $row = mysql_fetch_row($result); echo $row[0] . "<br >\n"; // username echo $row[1] . "<br >\n"; //postdate echo $row[2] . "<br >\n"; //post content Link to comment https://forums.phpfreaks.com/topic/188380-finding-username/#findComment-994545 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.