Iman Marashi Iman Marashi - 6 months ago 13
SQL Question

How to delete expired records?

I use this code to delete expired records in

current_questions
table(expire time is a minute):

$serverTime=mysql_query("SELECT NOW()")or die(mysql_error());
mysql_query("DELETE FROM current_questions WHERE TIMESTAMPDIFF( MINUTE ,`createTime`,'$serverTime')")or die(mysql_error());


This code does not work but it does not give any error.

How do records that have passed since the creation of one minute I remove them.

Field
createTime
to format this. 2016-05-13 01:24:05

Answer

mysql_query function returns a resource.

you still need to go and fetch the data from the resource

$serverTime = mysql_query("SELECT NOW()") or die(mysql_error());
$data = mysql_fetch_array($serverTime);
$serverTime = $data[0]; // and from now it should work.
mysql_query("DELETE FROM current_questions WHERE TIMESTAMPDIFF( MINUTE ,`createTime`,'$serverTime')")or die(mysql_error());

The other answer is better, but it's good to go back and learn from your own mistakes step by step.

Another thing, you should use mysqli extension instead of mysql. That one is older and soon will be deprecated.

Resources:

http://php.net/manual/en/function.mysql-fetch-array.php

http://php.net/manual/en/function.mysql-query.php