Cronjob有时无法执行SQL

时间:2017-06-10 09:36:40

标签: php sql mysqli cron

好的,所以我测试了一个可以在我的网站上运行的cronjob,但似乎它有时无法插入新数据......

基本上我发现每个类别中的哪辆车赢了最多而不是战斗,并将这些数据插入到我用来显示昨天获胜者的表格中。

的Cron

0 0 * * *

要执行的Php代码

$arr;
$stmt = $dbCon->prepare(" SELECT cars_versus_identifier, cars_versus_source, cars_versus_battles, cars_versus_won, cars_versus_category FROM cars_versus ");
$stmt->execute();
$stmt->bind_result($identifier, $imageSource, $battles, $won, $category);
$winners = array();
while ($stmt->fetch()) {
    $arr[] = array('category' => $category, 'id' => $identifier, 'versus' => $versus, 'won' => $won);
     if(isset($winners[$category])) {
          $curmax = $winners[$category]['won'];
          if($won > $curmax) {
                $winners[$category] = array('id' => $identifier, 'won' => $won);
          }
     } else {

         $winners[$category] = array('id' => $identifier, 'won' => $won, 'imageSource' => $imageSource);
    }
}
$stmt->close();
print_r($winners);

$stmt = $dbCon->prepare("TRUNCATE TABLE cars_winners");
$stmt->execute();
$stmt->close();

脚本的这部分有时会失败

foreach($winners as $key => $value){
    $id = $winners[$key]['id'];
    $imageSource = $winners[$key]['imageSource'];


    $stmt = $dbCon->prepare(" UPDATE cars_car "
            . " SET cars_car_versus = cars_car_versus + 1 "
            . " WHERE cars_car_id = ?");
    $stmt->bind_param('i', $id);
    if ($stmt->execute()) {
        echo "Success Versus <br>";
    } else {
        echo "Failed Versus <br>";
    }
    $stmt->close();
    ////////////////////////////////
    //THIS PART IS SOMETIMES FAILING
    ////////////////////////////////
    $stmt = $dbCon->prepare(" INSERT INTO cars_winners (cars_winners_source) "
            . " VALUES (?) ");
    $stmt->bind_param('s', $imageSource);
    if ($stmt->execute()) {
        echo "Success Versus winners <br>";
    } else {
        echo "Failed Versus winners <br>";
    }
    $stmt->close();
}

$stmt = $dbCon->prepare("TRUNCATE TABLE cars_versus");
$stmt->execute();
$stmt->close();

这是我的网站的一个关键部分,绝不能失败,我该怎么做才能防止它失败?

我注意到一些奇怪的事情,如果我只是运行cronjob而没有投票任何图像,它每次都会成功..但是如果我对每日汽车进行评分,它通常会在foreach循环中失败一个或两个SQL查询

成功的形象 enter image description here

有时失败的图像(这次都失败了) enter image description here

0 个答案:

没有答案