Jump to content

Delete row automatically when mysql_num_rows = X


cordoprod

Recommended Posts

Hi.

 

I develope a community system and i have a table called last_visitors..

 

The structure goes like this:

user - visitor - time

 

Lets say there is a row:

glenn - frank - (timestamp from time(); )

 

And when i have like 10 rows where the user is glenn automatically delete the oldest row..

(e.g find that from time col)

 

Do you know how to do that?

<?php
$time= time();
$sql=mysql_query( "INSERT INTO table values (user,time)");
while($row=mysql_fetch_array($sql)) {

if($row['time']>=15778463) { // deletes user if user time is greater than say 6 months

$sql=mysql_query"(DELETE FROM table VALUES (user,time)");
}
}
?>

<?php
$sql = mysql_fetch_array(mysql_query("SELECT COUNT(user) as numusers, MIN(time) as oldest FROM last_visitors"));
if ($sql['numusers'] == 10){
mysql_query("DELETE FROM last_visitors WHERE time = '$sql[oldest]'") or die(mysql_error());
}
$sql = mysql_query("INSERT INTO last_visitors VALUES ('$guestname','$visitorname','$time')") or die(mysql_error());
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.