Jump to content

Archived

This topic is now archived and is closed to further replies.

Lenbot

Query Help! perty please with a cherry on top!

Recommended Posts

I have a news table and a Primary key on it. the table is pretty simple set up like so

PrimaryKey, NewsTitle,NewsArticle,DateStamp


I 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.