Need some MySQL help

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
I installed Invision Board on my website and I've been playing around with using the forum post news updates for my site. Instead of using a pre-made modification or hack, I want to try to write my own code.

Information regarding posts are basically stored in two tables: ibf_topics and ibf_posts. ibf_topics has the thread titles and ibf_posts has the content of the threads.

Right now, I have this:

$MaxPosts = 5;
$query = "select * from ibf_posts where forum_id=6 and new_topic = 1 order by post_date desc limit 0, $MaxPosts";
$result = mysql_query("$query") or
die (mysql_error());

while ( $row = mysql_fetch_array($result) )
{ ..... }

From the results, I also get the thread id which I can use to identify the corresponding thread title for the post. How should I go about getting information from both tables? Should I repeatedly query ibf_topics from inside the while loop? Can I query two tables at once and have MySQL return all the information in one array? Thanks.
 

kt

Diamond Member
Apr 1, 2000
6,031
1,346
136
If you need to get information from both tables, you need to join them first. In your case, I would gather you will be joining the tables based on the thread ID so all the rows containing the posts for the particular thread are selected.
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
Could you explain what you mean by "joining" the tables? Is that done through code or MySQL admin? Thanks.
 

kt

Diamond Member
Apr 1, 2000
6,031
1,346
136
It's done thru your SQL statement. For example,

"SELECT * FROM ibf_topics JOIN ibf_posts ON ibf_topics.topicID=ibf_posts.topicID WHERE ibf_topics.topicID=1";

will return all posts with the topicID=1 and all relevant information (like topic title) are selected.