Page 1 of 1

php+mysql select and order

Posted: Sun Sep 12, 2004 12:40 pm
by PringlesOriginal
So this is what my 'threads' table looks like (which is under the database forums').

Code: Select all

threadID   	  forumID   	  creatorID   	  topic   	  firstPost   	  timeCreated   	  lastPostTime
and I'm sure (through phpmyadmin) that all data is being store there properly.

when i execute this command

Code: Select all

function lastTopic($forumID)
{
	$user="root";
    	$host="localhost";
    	$password="++++++";
	$database="foums";
    	$connection=mysql_connect($host,$user,$password) or die ("Couldnt connect to server");
    	$db=mysql_select_db($database,$connection) or die ("Couldnt select database");

	$query="SELECT * FROM threads WHERE forumID='$ForumID' ORDER BY lastPostTime DESC";
    $result=mysql_query($query) or die ("Query failed to get the last topic");
	$data=mysql_fetch_array($result);
	$lasttopic=$data['topic'];
	return $lasttopic;

}
the function returns nothing, i guess the sorting is not working....

Can anybody help me?

Posted: Sun Sep 12, 2004 12:44 pm
by feyd
what's $query rendered as? i.e. echo $query and post it.

that'd be a lot of queries once you get several threads or forums.. you might want to free the results (at the least) and do them in one hit.

Posted: Sun Sep 12, 2004 12:47 pm
by PringlesOriginal
whn i do a straigt echo of query the output on my browser is this:
SELECT * FROM threads WHERE forumID='' ORDER BY lastPostTime DESC

Posted: Sun Sep 12, 2004 12:49 pm
by feyd
when the function is called?

Posted: Sun Sep 12, 2004 12:51 pm
by PringlesOriginal
i call this in the main program like so:

Code: Select all

$forum1LastTopic=lastTopic(1);

Posted: Sun Sep 12, 2004 4:23 pm
by MarK (CZ)
you have capital 'F' on line 9.

Change

Code: Select all

<?php
$query="SELECT * FROM threads WHERE forumID='$ForumID' ORDER BY lastPostTime DESC";
?>
to

Code: Select all

<?php
$query="SELECT * FROM threads WHERE forumID='$forumID' ORDER BY lastPostTime DESC";
?>
$ForumID >> $forumID

EDIT: Or change the function parameter name to $ForumID. Just have it in the same case.

:wink:

Posted: Sun Sep 12, 2004 4:39 pm
by feyd
heh, good catch MarK! :lol:

Posted: Sun Sep 12, 2004 4:41 pm
by MarK (CZ)
Sure :wink:

Posted: Sun Sep 12, 2004 7:19 pm
by PringlesOriginal
thanks man! silly mistake :oops: