php+mysql select and order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PringlesOriginal
Forum Newbie
Posts: 10
Joined: Tue Sep 07, 2004 11:49 am

php+mysql select and order

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
PringlesOriginal
Forum Newbie
Posts: 10
Joined: Tue Sep 07, 2004 11:49 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

when the function is called?
PringlesOriginal
Forum Newbie
Posts: 10
Joined: Tue Sep 07, 2004 11:49 am

Post by PringlesOriginal »

i call this in the main program like so:

Code: Select all

$forum1LastTopic=lastTopic(1);
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

heh, good catch MarK! :lol:
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Sure :wink:
PringlesOriginal
Forum Newbie
Posts: 10
Joined: Tue Sep 07, 2004 11:49 am

Post by PringlesOriginal »

thanks man! silly mistake :oops:
Post Reply