Jump to content


Photo

Query Trouble


  • Please log in to reply
7 replies to this topic

#1 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 19 March 2006 - 06:06 PM

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.

    $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>";
      }


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 March 2006 - 08:11 PM

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] * FROM f_post, f_thread ORDER BY timep DESC 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.

#3 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 19 March 2006 - 11:39 PM

Alright thanks hitman, I'll try that out and let you know if it works.

#4 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 19 March 2006 - 11:50 PM

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

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 March 2006 - 12:08 AM

in your code, change your mysql_query line to something like:

$result = mysql_query($query) or die(mysql_error());

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

#6 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 20 March 2006 - 12:23 AM

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.

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 March 2006 - 12:35 AM

[!--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) View Post[/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.

#8 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 20 March 2006 - 12:50 AM

Yeah I fixed that actually, but now it's just posting the titles for the started thread, and not the reply title.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users