php - Select fields using Foreign keys in relational table -


this question exact duplicate of:

i trying display topic create (username) have go through relation table retrieve it. have created query displays creator (username) of reply, believe need sub-query have never used 1 before.

what trying use foreign keys retrieve username, hope below explains it:

forum_replies.topic_id >>>>> forum_topics.topic_id , forum_topics.user_id >>>> users.user_id.

the tables 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 code 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                        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 topic, first have 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 deprecated have been asked use them uni staff. greatful help. thanks

add join:

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,      users.user_id topic_user_id, users.username topic_username,  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     left join users u2 on forum_topics.user_id = u2.user_id 

...and mentioned in other question: use aliases


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -