Jump to content


Photo

Last 5 entries with php and mysql


  • Please log in to reply
5 replies to this topic

#1 wipe

wipe
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 03 July 2006 - 10:33 PM

Hi guys,

i have a small question.

Im using a script that display all results in a DB and i can do some query.
i insert also date.

Now i need to know how can i display with a echo the last 5 posts into my DB using date. How can i do that?

Regards,
Wipe

#2 birdie

birdie
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationBirmingham UK

Posted 03 July 2006 - 10:36 PM

SELECT * FROM table ORDER BY date DESC ?

#3 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 03 July 2006 - 10:53 PM

SELECT * FROM table ORDER BY date DESC ?


Wouldn't this just show the last five entry's for the "table".
How would you do this for your whole database?
(check all your tables)

[url=http://www.slimphoenix.com][/url]

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 04 July 2006 - 05:19 AM

I honestly don't know if there is a more efficient way than this (if there is, Barand would know :) ), but here's my solution:

let's say you have 3 tables called

table1
table2
table3

and in each table is date column. it doesn't matter if they are all named the same or not, as long as they are the same data type.  but let's just say for the sake of clarity they are all named different:

date1
date2
date3

and we want to choose the 5 most recent dates out of all 3 tables overall, right? okay so here we go:

(SELECT date1 FROM table1) UNION (SELECT date2 FROM table2) UNION (SELECT date3 FROM table3) ORDER BY date1 DESC LIMIT 5

okay so here's an explanation of what this does: this runs 3 seperate queries all at once.  Then it merges all the data into one column.  The column is named the same name as what is selected in the first query so since that is date1, the result will return your list with the column named date1.  That is why we are ordering by date1 at the end. 

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 zawadi

zawadi
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationChippenham UK

Posted 04 July 2006 - 10:59 AM

you would need to loop over all the tables in turn, to get the last 5 out
select * from table where date = 'daet here' limit 5

That's the beauty of asymmetric warfare. You don't need a lot of money, or an army of people. You can do it from the comfort of your living room, with a beer in your hand.

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 04 July 2006 - 11:14 AM

An added comment from the above example union will only work if the same information is in all table fields.

that what union does just joins all table fields as one.

lol...................... sorry
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users