Jump to content


Photo

Query Help! perty please with a cherry on top!


  • Please log in to reply
3 replies to this topic

#1 Lenbot

Lenbot
  • Members
  • Pip
  • Newbie
  • 5 posts
  • LocationSt. Thomas, ontario

Posted 26 June 2006 - 10:39 PM

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

#2 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 26 June 2006 - 10:53 PM

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:
SELECT NewsTitle,NewsArticle,DateStamp FROM news ORDER BY DateStamp DESC LIMIT 0, 5;

If I have it right, it should return the last 5 articles


Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )

#3 Lenbot

Lenbot
  • Members
  • Pip
  • Newbie
  • 5 posts
  • LocationSt. Thomas, ontario

Posted 26 June 2006 - 11:17 PM

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

#4 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 27 June 2006 - 12:36 AM

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.
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!
  }

}

Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users