zuc0001
zuc0001

Reputation: 930

Trouble with PHP Strings and SQL

I am a beginner at PHP, but have been attempting a project on a forum. I have come across a problem though.

            $getLatest = dbquery("SELECT * FROM site_forum_thread_comments,site_forum_threads,site_forum_subs WHERE site_forum_thread_comments.thread_id=site_forum_threads.thread_id AND 
        site_forum_threads.sub_id=site_forum_subs.sub_id AND
        site_forum_subs.main_id='".$forum['ID']."'
        ORDER BY site_forum_thread_comments.timestamp DESC LIMIT 1");
            while ($latest = mysql_fetch_assoc($getLatest))
                {
                    $getUser = dbquery("SELECT * FROM users WHERE id='".$latest['user_comment_id']."'");
                    while ($user = mysql_fetch_assoc($getUser))
                    {
                            echo ' <BR>Latest Post: '.$latest['comment'].'
                            <BR>User: '.$user['username'].'
                            <BR>Forum: '.substr($latest['site_forum_threads.title'],0,20).'...';
                    }
                }

This is just a snippet of the code. What happens is, $getLatest gets the code from the SQL query which are from a number of different tables in my database. My problem: At the last part Forum '.substry($latest['...blahblahblah']

Am I able to add the table function in it? So I can retrieve the column "title" from site_forum_threads as I have done it? Because in one of my other tables, there is also a column called "tables", and the query is getting mixed up between which column it should be getting the data from.

Thanks in advance!

EDIT : @zerkms

$getLatest = dbquery("SELECT * FROM site_forum_thread_comments,site_forum_threads,site_forum_subs 
        site_forum_threads.title AS title_from_site_forum_threads 
        WHERE site_forum_thread_comments.thread_id=site_forum_threads.thread_id AND 
        site_forum_threads.sub_id=site_forum_subs.sub_id AND
        site_forum_subs.main_id='".$forum['ID']."' AND
        side_forum_threads.title= title_from_site_forum_threads
        ORDER BY site_forum_thread_comments.timestamp DESC LIMIT 1");
            while ($latest = mysql_fetch_assoc($getLatest))
                {
                    $getUser = dbquery("SELECT * FROM users WHERE id='".$latest['user_comment_id']."'");
                    while ($user = mysql_fetch_assoc($getUser))
                    {
                            echo ' <BR>Latest Post: '.$latest['comment'].'
                            <BR>User: '.$user['username'].'
                            <BR>Forum: '.substr($latest['title_from_site_forum_threads'],0,20).'...';
                    }
                }

Upvotes: 1

Views: 68

Answers (1)

zerkms
zerkms

Reputation: 254926

Yes, just specify the name explicitly and alias it

site_forum_threads.title AS title_from_site_forum_threads

and access it using $latest['title_from_site_forum_threads']

The select part in total could look like

SELECT *, site_forum_threads.title AS title_from_site_forum_threads FROM ...

but personally I recommend you to always select only necessary things, by enumerating all the fields separated by comma manually

Upvotes: 1

Related Questions