1

My database has 3 tables i wish to access in the select query but I cannot seem to get it to work. Selecting from 2 tables works fine so I know everything else is working apart from my code for selecting from 3 tables. My database has been created on PHPmyadmin

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

This is the code I have tried to use and shows the fields I wish to select:

    $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.topic_id,forum_topics.topic_title, forum_topics.topic_date
                       FROM forum_replies
                       JOIN forum_topics
                       ON forum_replies.topic_id = forum_topics.topic_id
                       JOIN users
                       ON forum_replies.user_id = users.user_id

                       ";


        $result = mysql_query($queryreply) or die (mysql_error());
        $row = mysql_fetch_array($result); 

Example in code would be appreciated. Thanks

  • *cannot seem to get it to work*... what is your expected result? also, stop using deprecated `mysql_*` functions; use PDO / MySQLi instead. last, make good use of table alias. – Raptor Apr 23 '15 at 02:35
  • Yeah I understand it's deprecated but my university I'm studying at has told me to do so. I won't use in the future. I am at this moment just trying to echo out the $row to show all the replies to a topic on my forum website. With user details showing who posted them – phpdrivesmemad Apr 23 '15 at 02:39
  • 3
    What error are you getting? – Chip Dean Apr 23 '15 at 02:41
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.topic_id,forum_topics.topic_title, forum_topics.topic_date FROM forum_' at line 3 – phpdrivesmemad Apr 23 '15 at 02:42
  • You are missing a comma `,` between `users.username forum_topics.topic_id` – Sean Apr 23 '15 at 02:43
  • Dam, thanks you so much. the comma was the error. I have literally been working straight for 24 hours and need some fresh eyes to look over my code. Thank all you guys for your help! Feel silly now! – phpdrivesmemad Apr 23 '15 at 02:45

2 Answers2

0

You miss the , after users.username..

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.topic_id,forum_topics.topic_title, forum_topics.topic_date
Bla...
  • 7,228
  • 7
  • 27
  • 46
0

Use this query:

SELECT a.reply_text, a.reply_date, b.topic_title, c.username
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed

Apart from syntax errors, you should use LEFT JOIN and table alias in your case.


To show also the topic creator's username, you can adjust the query to the following:

SELECT a.reply_text, a.reply_date, b.topic_title, c.username AS reply_user, (SELECT username FROM users WHERE user_id=b.user_id) AS topic_creator
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • 1
    ironically you have a syntax error in `a,reply_text` – Sean Apr 23 '15 at 02:47
  • Thanks for your help, may I ask why left join instead of join? – phpdrivesmemad Apr 23 '15 at 02:49
  • You can refer to this: [Difference between JOIN (default is INNER JOIN) and LEFT JOIN](http://stackoverflow.com/questions/9770366/difference-in-mysql-join-vs-left-join). In your case, topic replies are the key information you want to obtain. If you use INNER JOIN, if topic / user is missing somehow, the replies won't be shown. – Raptor Apr 23 '15 at 02:52
  • Thanks I understand it now, I was wondering if you could help me with this new problem i'm facing. My forum page currently shows the forum title and all the replies to that specific forum (As well as the usernames of those who have replied) but i am also trying to show the username of the person who created the forum title, but I can't seem to figure it out. – phpdrivesmemad Apr 23 '15 at 04:19
  • updated the answer. You can perform a sub-query to find out the topic creator – Raptor Apr 23 '15 at 04:24
  • I've never used a sub-query before, I didn't want to just ask you to do it for me so have spent the last hour trying to put something together but cannot understand any of it. Could you please explain to me how I would retrieve username of topic creator through topic id referenced in forum_replies. Any help would be appreciated, I've updated the answer – phpdrivesmemad Apr 23 '15 at 05:07
  • As the selected topic creator is now named as `topic_creator` from the SQL, you can retrieve it by `$row['topic_creator']`. Have fun with MySQL! – Raptor Apr 23 '15 at 06:08
  • 1
    Thank you so much, I'm new to coding so thank you for putting up with me, I really appreciate it. – phpdrivesmemad Apr 23 '15 at 06:39
  • this is really not fair to open a new question and asking the same again :-( http://stackoverflow.com/questions/29814618/select-fields-using-foreign-keys-in-relational-table – niyou Apr 23 '15 at 06:46