I have a forum and im looping through the replies to a thread, and for each instance its querying the members database for the users info.
I’d like to do it in one query if possible. What kind of sql join do I need for this?
Here’s my existing code:
$sql_result = mysql_query("SELECT * FROM forum_replies WHERE topic='$topic_id' AND del=0 ORDER BY date asc", $db);
while($rs = mysql_fetch_array($sql_result))
{
$sql_result2 = mysql_query("SELECT * FROM members WHERE id='$rs[author]'", $db);
$rs2 = mysql_fetch_array($sql_result2);
Queries:
SELECT * FROM forum_replies WHERE topic='$topic_id' AND del=0 ORDER BY date asc
SELECT * FROM mobsters WHERE id='$rs[author]'"
I guess you are looking for something like this:
You want to select the replies and the data of the author alongside with every row.
:topic_idis the place for your topic ID (a notation used in PDO which you should look up and use instead of themysql_functions).I have an important suggestion though: instead of
select *, you should specify what you really want to return from your tables, what you really need.For example: