Reversing database reading

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
Z3r0Tol3ranc3
Forum Newbie
Posts: 7
Joined: Mon Jul 01, 2002 11:44 am

Reversing database reading

Post by Z3r0Tol3ranc3 »

i have made a newspage and i want the last inserted news to stand on the top of the page. like this then:

news item 4
news item 3
news item 2
news item 1

but now it is like

news item 1
news item 2
news item 3
news item 4

pls help me out.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what database?
for mysql you can use SELECT .... ORDER BY <your-field> DESC
Z3r0Tol3ranc3
Forum Newbie
Posts: 7
Joined: Mon Jul 01, 2002 11:44 am

Post by Z3r0Tol3ranc3 »

can you Xplain that more precycly, i'm new at PHP
some info, don't know that it helps you out

include("config.inc.php");
mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
$query="SELECT * FROM news";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$query="SELECT * order by news"; then or what??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

let's assume you have an auto-increment field called 'newsId' and it is the primary index of your table.
Then

Code: Select all

...
$result = mysql_query('SELECT topic FROM news', $conn); // or similar
while($row=mysql_fetch_row($result))
  myPrintTopic($row);
...
does what you've posted because the results are ordered by the primary index in ascend-direction (default)

Then reversing the order will be done by

Code: Select all

...
$result = mysql_query('SELECT topic FROM news ORDER BY newsId DESC', $conn);
while($row=mysql_fetch_row($result))
  myPrintTopic($row);
...
descending order of newsId

or you use a date/time-field. It's the same if you fill it with
INSERT INTO .... (...,datePost,...) VALUES (...,now(),...)
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Try this...

Post by icesolid »

Try and use this code:

Code: Select all

<?php
include("config.inc.php");
mysql_pconnect("$host", "$user", "$pass");
mysql_select_db("$db");

$sql = "SELECT * FROM news ORDER BY id DESC";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) &#123;
echo $row&#1111;"title"];
&#125;
?>
Remember that you must have an id tag which is set to primary and is also set to auto_increment. Doing so makes it a lot easier to search through and print out fields in your database.
Post Reply