Only 5 records pr. user?!?! [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Only 5 records pr. user?!?! [SOLVED]

Post by jmansa »

I want to delete a record for a specific user if the user have over 5 records in my DB table... Lets say that a user is inserting he's sixt record I want the script to delete the oldest record for that user... Is that possible??? I have tryid this but it doesnt delete the records... It just inserts the new one:

Code: Select all

$query=mysql_query("SELECT * FROM oak_listen WHERE SpillerID=1"); 
if(mysql_num_rows($query) > 4) 
{ 
  mysql_query("DELETE * FROM oak_listen WHERE Dato IN(SELECT Dato FROM oak_listen ORDER BY Dato LIMIT 1)"); 
}
Hope somebody have a answer...
Last edited by jmansa on Mon Mar 31, 2008 5:14 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Only 5 records pr. user?!?!

Post by John Cartwright »

Code: Select all

$query=mysql_query("SELECT * FROM oak_listen WHERE SpillerID=1") or die(mysql_error()); 
if(mysql_num_rows($query) > 4) 
{ 
  mysql_query("DELETE * FROM oak_listen WHERE SpillerID = 1 ORDER BY Dato DESC LIMIT 1") or die(mysql_error()); 
} 
 
You need to ORDER BY column DESC, and no need for a subquery here.
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Re: Only 5 records pr. user?!?!

Post by jmansa »

Thanks... But it gives me this error when I get to the 6 record...
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM oak_listen WHERE SpillerID = 1 ORDER BY Dato DESC LIMIT 1' at line 1
Any Idea?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Only 5 records pr. user?!?!

Post by John Cartwright »

Oops, remove the * in the delete query
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Re: Only 5 records pr. user?!?! [SOLVED]

Post by jmansa »

Thanks... Finally... It works :-)
Post Reply