Jump to content

Archived

This topic is now archived and is closed to further replies.

SieRobin

Query Trouble

Recommended Posts

I have a forum on my website, and when you go to forum.php there is "Last Posts" that I've created to show who last posted what. Now what it displays is the Title of the reply and the Author. The problem I'm having is that in the MySQL Database there are two different tables, f_thread and f_post.

In f_threads it displays all the newly started threads f_post is just the replies to the threads, and they're ordered by a number containing. How can I make it display both newly started threads and replies on the Last Posts thing, so that when someone posts a new thread it also displays as a last post.

Perhaps there is a way to use one query to pull the tables both? I'm not sure, but hmmn, if someone could help I'd appreciate it.

[code]    $last="SELECT * FROM  f_post order by timep DESC limit 5";
    $last2=mysql_query($last) or die("Could not retrieve posts");
    print "</table>
    <br><table class='table'><tr class='headline'><td align='center' colspan='2'>Last Posts</td></tr>
    <tr><td class='mainrowb'>Title</td><td class='mainrowb'>Author</td></tr>";
    while ($last3=mysql_fetch_array($last2)) {
    print "<tr class='mainrow'><td><a href='forum.php?brd=$last3[bname]&msg=$last3[msg]'>$last3[title]</a></td><td><a href='profile.php?id=$last3[UID]'>$last3[author]</a></td></tr>";
      }[/code]

Share this post


Link to post
Share on other sites
You'll probably have to adjust your column names...but I think this will work:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]f_post,[/color] f_thread [color=green]ORDER BY[/color] timep [color=green]DESC[/color] LIMIT 5 [!--sql2--][/div][!--sql3--]

Instead of the SELECT *, select only the fields that you need from your tables...I don't know your column names from f_thread, but from f_post it looks like you would only need bname, msg, title, UID, author...so change the * to "f_post.bname, f_post.msg, f_post.title, f_post.UID, f_post.author" followed by the fields needed from the f_thread table, using the same "tablename.fieldname" format.

Share this post


Link to post
Share on other sites
Alright thanks hitman, I'll try that out and let you know if it works.

Share this post


Link to post
Share on other sites
Nope, this is what I get.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sierobin/public_html/forum.php on line 101

Share this post


Link to post
Share on other sites
in your code, change your mysql_query line to something like:

[code]$result = mysql_query($query) or die(mysql_error());[/code]

The important part being or die(mysql_error()); so that it will show you what is wrong with your query.

Share this post


Link to post
Share on other sites
Column 'timep' in order clause is ambiguous

That's what it says lol.

I think I might have gotten it figured out, but just one thing.

The only thing it's doing weird, is that it's using the same title as in the thread title. So the replies never log actual titles, hmmn.

Share this post


Link to post
Share on other sites
[!--quoteo(post=356531:date=Mar 19 2006, 07:23 PM:name=Christopher Robin)--][div class=\'quotetop\']QUOTE(Christopher Robin @ Mar 19 2006, 07:23 PM) [snapback]356531[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Column 'timep' in order clause is ambiguous
[/quote]

That means that that column exists in more than one table. Specify which table you want it to select from in your query by doing tablename.timep.

Share this post


Link to post
Share on other sites
Yeah I fixed that actually, but now it's just posting the titles for the started thread, and not the reply title.

Share this post


Link to post
Share on other sites

×

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.