good_evening
good_evening

Reputation: 21759

Mysql join for thread and its replies

Ok, I am willing to create a simple forum for my website. So, let's begin.

I guess thread table is going to be smth like this:

t_id, thread_name, who_created, when_created

And replies table is going to be smth like this:

r_id, t_id, who_wrote, what_wrote, when_wrote

And when somebody wants to view thread with replies, he clicks button which moves him to index.php?t_id=1 or smth like that. t_id means threads' table value t_id. What join query should I write for showing topic and its replies?

mysql_query('SELECT *
             FROM threads AS t
             INNER JOIN replies AS r
             ON t.t_id = r.t_id
             WHERE t.t_id = '.mysql_escape_strings($_GET['t_id']));

Is it right? Then how should I pull out all that data? With mysql_fetch_array? Then how? Any suggestions? Maybe better write 2 queries instead of JOIN? Thanks.

Upvotes: 1

Views: 117

Answers (1)

shamittomar
shamittomar

Reputation: 46692

Don't use a join in this case as it does not give any advantage and instead it will slow down the queries. Rather do two separate queries:

mysql_query('SELECT *
         FROM threads AS t
         WHERE t.t_id = '.mysql_escape_strings($_GET['t_id']));

And:

mysql_query('SELECT *
         FROM replies AS r
         WHERE r.t_id = '.mysql_escape_strings($_GET['t_id']));

This will be much faster.

Upvotes: 3

Related Questions