Jump to content

Recommended Posts

im trying to improve my code by using table joins but when i use the following code it just returns everything in the database rather than the results that equal $forum_id which is the get value 2. There should only be 2 results.

 

 $query = $db->query("SELECT 
                    ".DB_PREFIX."topics.topic_id,
                    ".DB_PREFIX."topics.topic_name,
                    ".DB_PREFIX."topics.topic_poster,
                    ".DB_PREFIX."topics.topic_time_posted,
                    ".DB_PREFIX."topics.topic_views,
                    ".DB_PREFIX."topics.topic_replies,
                    ".DB_PREFIX."topics.topic_last_poster,
                    ".DB_PREFIX."topics.topic_last_post_time,
                    ".DB_PREFIX."topics.topic_locked,
                    ".DB_PREFIX."topics.topic_sticky,
                    
                    ".DB_PREFIX."parents.parent_id,
                    ".DB_PREFIX."parents.parent_name,
                    
                    ".DB_PREFIX."forums.forum_name,
                    
                    ".DB_PREFIX."members.user_username,
                    ".DB_PREFIX."members.user_group
                    
                    FROM ".DB_PREFIX."topics
                    
                     JOIN ".DB_PREFIX."members
                     JOIN ".DB_PREFIX."parents
                     JOIN ".DB_PREFIX."forums
                    
                    ON ".DB_PREFIX."topics.topic_poster 
                    = ".DB_PREFIX."members.user_username
                    
                    WHERE ".DB_PREFIX."forums.forum_id
                    = ".$forum_id."
                    
                    ORDER BY ".DB_PREFIX."topics.topic_time_posted $max")
                    or trigger_error("SQL", E_USER_ERROR);

Link to comment
https://forums.phpfreaks.com/topic/214090-table-joins/
Share on other sites

because your select is doing a couple of JOINS without conditions ... this part has an incorrect format that cause the behavior that you are seeing

 

 

                    FROM ".DB_PREFIX."topics
                     JOIN ".DB_PREFIX."members
                     JOIN ".DB_PREFIX."parents
                     JOIN ".DB_PREFIX."forums
                    ON ".DB_PREFIX."topics.topic_poster 
                    = ".DB_PREFIX."members.user_username

 

it should be something like this (adjust to your table descriptions)

                    FROM ".DB_PREFIX."topics
                     JOIN ".DB_PREFIX."members 
                               ON ".DB_PREFIX."topics.topic_poster  = ".DB_PREFIX."members.user_username
                     JOIN ".DB_PREFIX."parents  ON ".DB_PREFIX."<complete here> = ".DB_PREFIX."<complete here>
                     JOIN ".DB_PREFIX."forums   ON ".DB_PREFIX."<complete here> = ".DB_PREFIX."<complete here>

                   

                   

Link to comment
https://forums.phpfreaks.com/topic/214090-table-joins/#findComment-1114095
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.