View Full Version : php/mysql question
I built a shoutbox type thing on my flash page...It works fine...but i want it to never have any more messages than 20...so i can check how many rows in the database..but how do i erase the 21st line.
I'm fairly new to sql...but fairly versed at php...thanks for reading
Eric
Why not use 'limit 20' at the end of your SQL string to only get 20 records back.
ie SELECT * FROM COMPANY ORDER LIMIT 20;
You can then delete the extra records at your leisure.
freddycodes
04-07-2004, 12:57 PM
Yeah you will need to tag each one with a timestamp, I assume you have a field for time posted. So you can get the latest 20
select * from tablename order by timestampfield LIMIT 20
Yeah you will need to tag each one with a timestamp, I assume you have a field for time posted. So you can get the latest 20
select * from tablename order by timestampfield LIMIT 20
Ya thats the sql query i have right now....But how do i erase the rest....
freddycodes
04-07-2004, 05:41 PM
Why do you care about erasing them. Its a database.
Das123
04-08-2004, 03:41 AM
In php you can set up a query that gives you $myRowCount of the table.
If $myRowCount > 20 then $numberToDelete = $myRowCount - 20
and run a query...
DELETE FROM myTable ORDER BY timestamp LIMIT $numberToDelete
That will delete all the older rows other than the last 20.
Why do you care about erasing them. Its a database.
Because I'm going to deleted the extraneous shouts anyway, I'd much rather have the process automated
In php you can set up a query that gives you $myRowCount of the table.
If $myRowCount > 20 then $numberToDelete = $myRowCount - 20
and run a query...
DELETE FROM myTable ORDER BY timestamp LIMIT $numberToDelete
That will delete all the older rows other than the last 20.
Ok, thank you very much
freddycodes
04-08-2004, 01:14 PM
Well do whatever you want, but if you are only storing a set number, why not just use a text file, why use the overhead of a database for 20 records?
Well do whatever you want, but if you are only storing a set number, why not just use a text file, why use the overhead of a database for 20 records?
haha well this is a pretty bad answer...it was supposed to be a more verbose shoutbox system..then the client changed their mind and only wants 20....And since i already have it built i figured..why not make it work
freddycodes
04-08-2004, 02:43 PM
Okay so why not leave them in there, and down the road when they say we want to look at history, you can show them historical entries, its not going to hurt anything, mysql can handle thousands of records, and you are only grabbing 20 at a time.
hswaseer
05-07-2004, 10:24 AM
yes dear freddycodes is right .... it will be useful when u have to see the old records... You can write a function which displays only 20 records and the rest can be saved in the database.
HS
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.