Reputation: 11
I am new in PHP and MySql. I want to delete rows from a table where order_before passed the current date and time. the order_before is date/time type.
Upvotes: 0
Views: 830
Reputation: 173652
Just add the condition in the statement, should be easy enough:
$sql = sprintf("DELETE FROM mytable WHERE order_before > '%s'", date('Y-m-d H:i:s'));
$db->exec($sql);
Upvotes: 1
Reputation: 3559
<?php
$db = new PDO('mysql:host=localhost;dbname=db','root','root');
if(filter_has_var(INPUT_POST, 'submit')) {
$now = date("Y-m-d H:i:s");
$r = $db->query("SELECT * FROM db WHERE datetime < '".$now."'");
$n = $r->rowCount();
if ($n){
while($o = $r->fetchObject()) {
$db->query('DELETE FROM db WHERE id = '.$o->id);
}
}
}
Maybe something like the above? (untested, and off the top).
Upvotes: 2