Jump to content


Photo

Record Retrieval from Database


  • Please log in to reply
6 replies to this topic

#1 markspec87

markspec87
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 06 June 2006 - 02:51 PM

Hey guys,

What i need to do is select the two last added articles from the table "news" and display them on my front page.

I need to, say, when i have selected the 2nd last added item display only one particular column. I.e the newstext column.

[a href=\"http://img339.imageshack.us/img339/3136/example0fh.jpg\" target=\"_blank\"]http://img339.imageshack.us/img339/3136/example0fh.jpg[/a]

And when a new item is added to DB, the news will drop down one, and the old "2nd to last added" will drop off.

I am unsure on how to select both an individual record field, like "Newstext" and also how to select say the 2nd or 3rd last added entry.

Sorry for bad explanation but thats the best i can do :)

hope you guys can help me out here. Ive been stuck on this for a week or so.


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 06 June 2006 - 02:55 PM

SELECT * FROM table ORDER BY newsdate DESC LIMIT 2

Just another note: "date" is a bad column title because it's a MySQL and PHP keyword.

Info: PHP Manual


#3 markspec87

markspec87
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 06 June 2006 - 04:00 PM

yeh in my actual databse is called "datum".

So Say in my table on my website.

What code would i use to display the 2nd last record from the column
"newstext". or the 2nd last date a news page was published.

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 06 June 2006 - 05:10 PM

if($result && mysql_num_rows($result) > 0) // error checking
{
     while($row = mysql_fetch_array($result))
     {
          echo $row['column1'];
          echo $row['column2'];
     }
}

Info: PHP Manual


#5 markspec87

markspec87
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 06 June 2006 - 05:47 PM

[!--quoteo(post=380670:date=Jun 6 2006, 06:10 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Jun 6 2006, 06:10 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
if($result && mysql_num_rows($result) > 0) // error checking
{
     while($row = mysql_fetch_array($result))
     {
          echo $row['column1'];
          echo $row['column2'];
     }
}
[/quote]

thats almost what im looking for, however this outputs all the fields in those columns. I just want to output say the 2nd last added record, or the 3rd added record on thier own.

thanks for your help.

#6 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 06 June 2006 - 05:58 PM

SELECT * FROM table ORDER BY newsdate DESC LIMIT 1,1

That will get record #2

The limit clause uses an offset/count format.

Info: PHP Manual


#7 markspec87

markspec87
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 06 June 2006 - 11:12 PM

I put that in but then i get this error:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: mysql_result(): Unable to jump to row 2 on MySQL result index 5 in /home/.... file.php on line 110[/quote]

I definitely have the records in my DB to display so im a bit stuck.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users