Page 1 of 1

Returning One Result

Posted: Tue Feb 14, 2006 3:43 pm
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

Posted: Tue Feb 14, 2006 3:46 pm
by LiveFree
Add

Code: Select all

LIMIT 1
To the end of your query, the top record (#1) is always the newest one\

Posted: Tue Feb 14, 2006 3:48 pm
by psurrena
Works perfect, thanks!

Posted: Tue Feb 14, 2006 3:50 pm
by LiveFree
No Problem,

Happy Valentine's Day :)