PDA

View Full Version : Displaying mysql query


SilentK
Mon 16th May '05, 11:43am
I want to display the first post of the most recent thread on my non vbulletin frontpage. I setup up my frontpage so it required global.php and
I have the following query.

<?php
$threads = $DB_site->query("
SELECT *
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid = 57
ORDER BY dateline DESC
LIMIT 1
");
?>

My question is how do I display that query? I tried echo and it didn't work.

Marco van Herwaarden
Mon 16th May '05, 12:29pm
$threads = $DB_site->query_first("
SELECT *
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid = 57
ORDER BY dateline DESC
LIMIT 1
");
echo "$threads[title]";

SilentK
Mon 16th May '05, 4:06pm
Hmmm ok that's definitly a step forward seeing as I can now grab the title.
Is it possible to get the actually post though by doing $threads[????]? Or do I need to do a different query?

*edit* nevermind I just looked at the thread table structure and I can see that I will have to do another query.

I should be able to use $threads[firstpostid] in the query to get the relating post correct?

*edit#2* I got it to work with
$post = $DB_site->query_first("
SELECT *
FROM " . TABLE_PREFIX . "post AS post
WHERE post.postid = $threads[firstpostid]
ORDER BY dateline DESC
LIMIT 1
");
echo "$post[pagetext]";

SilentK
Tue 17th May '05, 7:37pm
Hmmm I want to make it so users can make posts in the forum but they have to be moderated first so I need to expand the query so it only grabs visible posts.

<?php
$threads = $DB_site->query("
SELECT *
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid = 57
WHERE thread.visible = 1
ORDER BY dateline DESC
LIMIT 1
");
?>I am not familer enough with sql qeuries to know if I can do two WHERE's or not so would that work? I am going to go try it.

Marco van Herwaarden
Wed 18th May '05, 1:40am
Just use AND or OR to seperate 2 conditions, like:
<?php
$threads = $DB_site->query("
SELECT *
FROM " . TABLE_PREFIX . "thread AS thread
WHERE thread.forumid = 57
AND thread.visible = 1
ORDER BY dateline DESC
LIMIT 1
");
?>

PS You can find the documentation at www.mysql.com (http://www.mysql.com) very usefull if you want information about the SQL syntax.