Jump to content

[SOLVED] MySQL: read from the bottom to the top of the table?


clown[NOR]

Recommended Posts

is there an easy way to do this or do I have to just loop it like i do now?

 

this is what i'm using now...

<?php
## Get the last 5 news...
function freshNews() {
	global $dbHost, $dbUser, $dbPass, $dbName;
	if (!mysql_connect($dbHost, $dbUser, $dbPass)) { die("Unable to connect to DB"); }
	if (!mysql_select_db($dbName)) { die("Unable to select DB"); }

	$query = "SELECT * FROM news";
	$result = mysql_query($query);

	if (!$result) { die("Could not run query from DB"); }

	$dbNumRows = mysql_num_rows($result);

	if ($dbNumRows > 0) {
		$i = 1;
		while ($dbNumRows > 0) {
			if ($i <= 5) {
				$query = "SELECT * FROM news WHERE nid = '".$dbNumRows."'";
				$result = mysql_query($query);
				$newsItem = mysql_fetch_assoc($result);
				if (strlen($newsItem['title']) > 30) {
					echo '<a href="?view=news&id='.$newsItem['nid'].'">'.substr($newsItem['title'], 0, 30)."...</a><br>";
				} else { echo '<a href="?view=news&id='.$newsItem['nid'].'">'.$newsItem['title']."</a><br>"; }
				$dbNumRows = $dbNumRows - 1;
				$i++;
			} else { break; }
		}
	} else { echo "No news found."; }

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.