Returning One Result

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
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Returning One Result

Post by psurrena »

My question is:

The code below works great for returning a list of current articles but I would like the homepage to just display the newest article. How do I make the code return only the single most current item in my database?

Code: Select all

if(!isset($_GET['id'])) 
{ 
    $self   = $_SERVER['PHP_SELF']; 

    $query  = "SELECT id, title, abstract FROM article ORDER BY id DESC"; 
    $result = mysql_query($query) or die('Error : ' . mysql_error());  
     
    while($row = mysql_fetch_array($result, MYSQL_NUM)) 
    { 
        list($id, $title, $abstract) = $row; 
        $content .= "
        			<b><a href=\"$self?id=$id\">$title</a></b><br />
        			$abstract<br /><br />
        			<a href=\"$self?id=$id\">Read Full Article...</a><br /><hr>\r\n"; 
    }
Thank you,
Peter
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Add

Code: Select all

LIMIT 1
To the end of your query, the top record (#1) is always the newest one\
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Works perfect, thanks!
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

No Problem,

Happy Valentine's Day :)
Post Reply