MySQL Forums
Forum List  »  Newbie

problems with proper ordering
Posted by: VanGuilder
Date: July 10, 2005 03:11PM

im trying to build a PHP script that will query a table called "news" and post it to my website. The code works just fine, except I want my results to be displayed in the reverse order of what my curent PHP code turns out. (ie I want it to display the five most recent entries)
This isnt a verbatum CnP of my code, but the only chunk that is fundimentally different in the code i for my site is where I removed some irrelevant style and formatting tags to keep the code simple for everyone else to read. ive looked toward otehr sources that indicated I should use SORT BY, but that returns multiple errors (one for every pass the code makes through my for loop) heres the snippet of code thats giving me all these problems

$db = mysql_connect('host', 'user', 'pass');

mysql_select_db("database",$db);

$result = mysql_query("SELECT * FROM table",$db);


echo "<table border=1>\n";

for ( $counter = 0; $counter <= 4; $counter += 1) {
$myrow = mysql_fetch_row($result);

printf("<tr><td align =\"left\">%s</TD> <TD align = right>%s</td></TR><TR><td colspan=2>%s</td></tr>\n",
$myrow[1], $myrow[2], $myrow[3]);

}


echo "</table>\n";

im using a dummy database on a remote MySQL server running version 4.0.13 that is set up like this: and yeah, I know i could make my title n date into one entry, but I dont because im too lazy to includ HTML every time I want to add to my table so i keep em seperate

dateorder title date text
(datetime) (Varchar) (varchar) (blob)
2005-07-10 16:48:56 Title One 00/00/00 - 00:00PM text for title one
2005-07-10 16:50:36 Title Two 00/00/00 - 00:00PM text for title two
2005-07-10 16:51:08 Title Three 00/00/00 - 00:00PM text for title three
2005-07-10 16:51:45 Title Four 00/00/00 - 00:00PM text for title four
2005-07-10 16:52:17 Title Five 00/00/00 - 00:00PM text for title five
2005-07-10 16:53:11 Title Six 00/00/00 - 00:00PM text for title six

I included the date-time that isnt displayed on the page so that each post would have a unique non-zero incremented identifier for ordering and sorting purposes. Can someone give me a hand with this? I need a solution that will reverse the order that this code displays output in so that the 5 most recent entries are PHP-ifyed into my page.

Options: ReplyQuote


Subject
Written By
Posted
problems with proper ordering
July 10, 2005 03:11PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.