swollenpickles Posted October 26, 2007 Share Posted October 26, 2007 Here's another question that's driving me nuts. I have created a php page (using mysql dbase) so people can leave comments on a comic (yeah, I like comics! ). I've figured out how to display these comments, and that works how I'd like. The thing is, when a user leaves a comment, it collects the comment, rating, date, title and memberid. All these things are stored in the membersreview table. The problem comes when trying to output the member name with the appropriate comment. The membername is stored in the member table, with the memberid (stored in the membersreview table) acting as a foreign key. My long winded question is, how do I output the relevant membername rather than just the memberid (which is just a number)? Here's the code I have that is working so far: <?php $query = "SELECT * FROM membersreview WHERE ComicId='3' ORDER BY ReviewDate ASC"; $resultSet = mysql_query($query); // returns assoc array of current row until there are no rows left while($row = mysql_fetch_assoc($resultSet)) { print "<table>"; print "<tr>"; print "<td>Member:</td>"; print "<td>" . $row["MemberId"] . "</td>"; print "</tr>"; print "<tr>"; print "<td>Review Date:</td>"; print "<td>" . $row["ReviewDate"] . "</td>"; print "</tr>"; print "<tr>"; print "<td>Rating:</td>"; print "<td>" . $row["UserRating"] . " out of 5</td>"; print "</tr>"; print "<tr>"; print "<td>User Comments:</td>"; print "<td>" . $row["UserComments"] . "</td>"; print "</tr>"; print "</table>"; } ?> I'd like to try and use a function to grab the appropriate username so i can use it in the output. Here's what I have (it doesn't work at the moment). Any idea how I could get this working? Please?? function getMemberName($memberid) { require_once("connection.php"); $query = "SELECT Member FROM member WHERE MemberId == Member"; $result = mysql_query($query); $memberdata = mysql_fetch_assoc($result); return $memberdata['memberName']; } Quote Link to comment https://forums.phpfreaks.com/topic/74867-displaying-username-from-foreign-key/ Share on other sites More sharing options...
GingerRobot Posted October 26, 2007 Share Posted October 26, 2007 You don't need to perform an additional query, you can do it with a join: <?php $query = "SELECT membersreview.*, member.Member AS MemberName FROM membersreview,member WHERE membersreview.ComidId=3 AND membersreview.MemberId=member.MemberId ORDER BY membersreview.ReviewDate ASC"; $resultSet = mysql_query($query) or die(mysql_error()); // returns assoc array of current row until there are no rows left while($row = mysql_fetch_assoc($resultSet)) { print "<table>"; print "<tr>"; print "<td>Member:</td>"; print "<td>" . $row["MemberName"] . "</td>"; print "</tr>"; print "<tr>"; print "<td>Review Date:</td>"; print "<td>" . $row["ReviewDate"] . "</td>"; print "</tr>"; print "<tr>"; print "<td>Rating:</td>"; print "<td>" . $row["UserRating"] . " out of 5</td>"; print "</tr>"; print "<tr>"; print "<td>User Comments:</td>"; print "<td>" . $row["UserComments"] . "</td>"; print "</tr>"; print "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74867-displaying-username-from-foreign-key/#findComment-378520 Share on other sites More sharing options...
swollenpickles Posted October 26, 2007 Author Share Posted October 26, 2007 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/74867-displaying-username-from-foreign-key/#findComment-378567 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.