Page 1 of 1

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

Posted: Mon Mar 31, 2008 4:18 pm
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...

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

Posted: Mon Mar 31, 2008 4:23 pm
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.

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

Posted: Mon Mar 31, 2008 4:32 pm
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?

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

Posted: Mon Mar 31, 2008 5:01 pm
by John Cartwright
Oops, remove the * in the delete query

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

Posted: Mon Mar 31, 2008 5:13 pm
by jmansa
Thanks... Finally... It works :-)