php - 删除时间戳最旧的值

标签 php mysql drupal drupal-7 cron

我希望当 cron 运行时删除最旧的时间戳值并同时创建新值。我有这个 cron 代码。这里选择看门狗的所有值:

function error_cron() {

// Begin building the query.
$query = db_select('watchdog', 'th')
  ->extend('PagerDefault')
  ->orderBy('wid')
  ->fields('th', array('variables', 'type', 'severity',
       'message', 'wid', 'timestamp'))
  ->limit(2000);

// Fetch the result set.
$result = $query -> execute();

// Loop through each item and add to $row.
foreach ($result as $row) {
  error_table($row);
}

在下一个代码中,我在表中创建看门狗的不同值:

function error_table($row) {

  $variables = $row -> variables;
  $timestamp = $row -> timestamp;
  $wid = $row -> wid;

  $r = db_select('error', 'b')
    -> fields('b')
    -> condition('variables', $variables, '=')
    -> condition('timestamp', $timestamp, '<=')
    -> execute();

  if($r -> rowCount() == 0) {
    $query = db_insert('error')
      ->fields(array(
        'timestamp' => $timestamp,
        'wid' => $wid,
        'variables' => $variables,
     ))
     ->execute();
  }
}

好的,所有这些代码都已创建表,并且它具有不同的值,但是当 cron 重新运行时,它显示相同的值..我希望当存在具有相同“变量”的行时删除具有最旧时间戳的行并使用最新的时间戳创建新的。

最佳答案

我用这行代码解决了我的问题:

$giannis = db_delete('error')
    -> condition('variables', $variables, '=')
    -> condition('timestamp', $timestamp, '<')
    ->execute();

关于php - 删除时间戳最旧的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072043/

相关文章:

javascript - 当我点击提交按钮时在 php 中定位我想转到两个不同的 URL

java - Ignite 和 MySQL - 更新条目数量意外

drupal - Drupal 7 有没有办法阻止管理员编辑节点?

mysql - Drupal 找不到请求的页面 "/"

php - 如何创建数据透视表 laravel?

php - CentOS Ioncube make_license 每次我尝试执行时都是 "Killed"

php - MySQL 多列 GROUP BY 查询

sql - 两个坐标之间的距离,我怎样才能简化这个和/或使用不同的技术?

php - 反序列化mysql表中的数据并通过php输出?

PHP/mysql 内存错误 Drupal