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 Link to comment https://forums.phpfreaks.com/topic/12965-query-help-perty-please-with-a-cherry-on-top/ 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 Link to comment https://forums.phpfreaks.com/topic/12965-query-help-perty-please-with-a-cherry-on-top/#findComment-49843 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 Link to comment https://forums.phpfreaks.com/topic/12965-query-help-perty-please-with-a-cherry-on-top/#findComment-49853 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] Link to comment https://forums.phpfreaks.com/topic/12965-query-help-perty-please-with-a-cherry-on-top/#findComment-49882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.