Jump to content

Query Help! perty please with a cherry on top!


Lenbot

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
Link to comment
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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.