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!
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:
$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.
$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.
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