Jump to content

[SOLVED] Delete row if time is more than


UQ13A

Recommended Posts

Hi,

how do i delete a row from my table if it is more than 24 hours old

 

my code to insert the row into the MYSQL database is

<?php
include("db.php"); // Connect to DB

$browser = $_SERVER['HTTP_USER_AGENT'];
$IP = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['PHP_SELF'];

$now = date("Y-m-d H:i:s", strtotime('+6 hours')); // My server is 6 hours behind my local timezone

mysql_query("INSERT INTO `[logs]`(`time`,`IP`,`browser`,`page`) values('$now','$IP','$browser','$page')") or die(mysql_error());

?>

 

So in other words...

if the row has been in the table for more than 24 hours i would like to delete it itself.

Can anybody help me out with a basic script

 

thanks

Link to comment
Share on other sites

you just need to check if now subtract date field is greater than or equal to 86400 (1 day in seconds)

 

You will need to set up a cron job script to do this automatically

 

 

thanks but how would i go around doin this?

Im a total newbie at PHP and MYSQL

 

is the script like this?

?<php
$logs = mysql_query("SELECT * FROM `[logs]`") or die(mysql_error());
$row = mysql_fetch_array($logs);

$dt = "86400";
$time = $row['time'];

if($time - $dt >= 86400) {
echo "Success";
} else {
echo "Error";
}
?>

 

I have tried it but i cant get it to echo "Success"

Link to comment
Share on other sites

Please ignore my above comment  :shrug:

 

here is my code

<?php

$logs = mysql_query("SELECT * FROM `[logs]` WHERE id = '1'") or die(mysql_error());
$row = mysql_fetch_array($logs);

$now = date("Y-m-d H:i:s", strtotime('+6 hours'));

$dt = "86400";
$time = $row['time'];

$diff = $now - $time;

if($diff >= $dt) {
print "Delete Now";
} else {
echo "Dont delete";
}

?>

Link to comment
Share on other sites

lol yes I know. I mean what field TYPE is the TIME field (such as int, varchar, float, decimal)? can you run code in phpmyadmin?

if so can you run the query

DESCRIBE logs

and paste the result

 

i couldnt seem to do it with [logs] so i copied the structure and data to a table called logs (from operations)

 

Field  Type  Null  Key  Default  Extra 

      id int(11) NO PRI NULL auto_increment

      page varchar(250) NO  none 

      browser varchar(250) NO  NULL 

      IP varchar(250) NO  NULL 

      time timestamp NO  CURRENT_TIMESTAMP 

      owner smallint(1) NO  1 

      ref_web varchar(255) NO  NULL

 

 

Link to comment
Share on other sites

I found this code while searching this website

 

<?php

mysql_query("DELETE FROM `logs` WHERE time + 86400 >= UNIX_TIMESTAMP()") or die(mysql_error());
echo "Deleted";

?>

 

But it deletes all rows from my table :wtf:

 

Link to comment
Share on other sites

Your operator is wrong. It would delete records which are less than 24 hours old. Plus, you need to convert the time field to seconds as well.

 

DELETE FROM `logs`
WHERE UNIX_TIMESTAMP(`time`) + 86400 < UNIX_TIMESTAMP()

 

 

Thanks for that but i can seem to get it to work in php it works in phpmyadmin

 

<?php
include("db.php");

$sql = 'DELETE FROM `logs` WHERE UNIX_TIMESTAMP(`time`) + 86400 < UNIX_TIMESTAMP()' or die(mysql_error());

if(($sql))
{

echo '<p>Logs Successfully Deleted!</p>';

} else {

echo '<p>Error</p>';
}

?>

 

and it doesnt give any errors

Link to comment
Share on other sites

Ah ha i found the error

 

fixed it

i changed the following line from

<?php
$sql = 'DELETE FROM `logs` WHERE UNIX_TIMESTAMP(`time`) + 86400 < UNIX_TIMESTAMP()' or die(mysql_error());
?>

to

<?php
$sql = mysql_query("DELETE FROM `logs` WHERE UNIX_TIMESTAMP(`time`) + 86400 < UNIX_TIMESTAMP()") or die(mysql_error());
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.