Lenbot Posted June 26, 2006 Share Posted June 26, 2006 I have a news table and a Primary key on it. the table is pretty simple set up like soPrimaryKey, NewsTitle,NewsArticle,DateStampI would usually query and pull the information from the news table but I find that very unefficient. I only want the last 5 articles added. I am not exactly sure how to even query for such a thing :(. Any help is highly appreciated and I would like to thank whom ever responds in advance :).Cheers -Lenbot Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted June 26, 2006 Share Posted June 26, 2006 What you need is LIMIT in the SELECT statement [a href=\"http://dev.mysql.com/doc/refman/5.0/en/select.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/select.html[/a]. To get the last 5 results try this:[code]SELECT NewsTitle,NewsArticle,DateStamp FROM news ORDER BY DateStamp DESC LIMIT 0, 5;[/code]If I have it right, it should return the last 5 articles Quote Link to comment Share on other sites More sharing options...
Lenbot Posted June 26, 2006 Author Share Posted June 26, 2006 Thanks man It seems to do the trick though I haven't fully tested it yet. Thanks in advance I think it will work.// Update (edited)///Ya it doesnt seem to work. Thanks for pointing me in the right direction perhaps I will have some success with the link you sent me.//Update agian//shouldn't this work, it should return atleast something right? $sqlquery = "SELECT * FROM NEWS";$result = mysql_query($sqlquery) or die('MYSQL Error : '.mysql_error());$returnedRecords = mysql_num_rows($result);for($i = 0; $i < $returnedRecords; $i++){$row = mysql_fetch_array($result);$TITLE = $row['NEWSTITLE'];$NEWS = $row['NEWSARTICLE'];$DATE = $row['NEWSDATE'];}If so why doesnt it. Is there something there I am missing Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted June 27, 2006 Share Posted June 27, 2006 Your problem is that the array that mysql_fetch_array returns an array with only number indices. You need to use MYSQL_ASSOC for the second argument of mysql_fetch_array().Here is the code that would I use if I were you.[code]if (mysql_num_rows($result) != 0) { while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $TITLE = $row['NEWSTITLE']; $NEWS = $row['NEWSARTICLE']; $DATE = $row['NEWSDATE']; // be sure to do your operation with $TITLE, $NEWS and $DATE here! }}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.