I want to make a cronjob which will execute every hour, select from two tables and if record isn't found in both tables and is older than 2 hours -> delete it.
So far I've build this simple delete statement but I realize that this will delete all records older than 2 hours because I don't check second table. So I need a bit help with how exactly I can select and if() then delete..
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select = "SELECT *
FROM orders
LEFT JOIN payments ON orders.order_id = payments.orderID
WHERE orders.created_at < DATE_SUB( CURDATE( ) , INTERVAL 1 HOUR )
AND payments.processed < 1";
$conn->exec($select);
if($select > 0)
// sql to delete a record
$sql = "delete from `orders` where ...)";
$conn->exec($sql);
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>
Obviously I had a problem with construction if condition part for deletion. Can you help me a bit with this?