Jump to content

Recommended Posts

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! :P). 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'];
		}

Link to comment
https://forums.phpfreaks.com/topic/74867-displaying-username-from-foreign-key/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.