简体   繁体   中英

MySql delete operation comparing datetime with current date time

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.

<?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).

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM