I am trying to display topic create (username) but have to go through a relation table to retrieve it. I have already created a query that displays the creator (username) of a reply, i believe i need a sub-query but have never used one before.
What i am basically trying to do is use the foreign keys to retrieve username, i hope below explains it:
Forum_replies.topic_id >>>>> forum_topics.topic_id and forum_topics.user_id >>>> users.user_id.
The Tables are as follows:
forum_replies
- reply_id
- topic_id
- user_id
- reply_text
- reply date
forum_topics
- topic_id
- category_id
- user_id
- topic_title
- topic_description
- topic_date
users
- user_id
- username
Here is my code which currently displays forum_topics.Topic_title, forum_replies.reply_date, forum_replies.user_id (shows username of reply creator), forum_replies.reply_text.
$queryreply = "SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username, forum_topics.user_id,
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
FROM forum_replies
LEFT JOIN forum_topics
ON forum_replies.topic_id = forum_topics.topic_id
LEFT JOIN users
ON forum_replies.user_id = users.user_id
";
$result = mysql_query($queryreply) or die (mysql_error());
$row = mysql_fetch_array($result);
if(empty($row['topic_id'])){
echo "No replies have been posted in this Topic, be the first to have your say using form below.";} ?>
<table id="categorytable">
<tr><td><?php echo '<b>'.$row['topic_title'].'</b>';?></b><br><br></td></tr>
<tr><td><?php echo $row['reply_date'].' - '.$row['username'].' Replied with: ';?><br><br></td></tr>
<tr><td><?php echo $row['reply_text'];?></td></tr>
I know mysql_* functions are deprecated but i have been asked to use them by uni staff. I would be greatful for any help. Thanks