cron does not update database

Asked

Viewed 25 times

0

I created a php file to check if there are any expired users or update their status with database update 1 time a day. I’m scheduling the cron for the hostgator’s CPANEL. If I run the file in the browser it does the process correctly, however by cron it always skips the mysql query going straight to the message (No customer to update status) even though.

PHP code

$hoje       = Date('Y-m-d h:i:s');
$menos30    = Date('Y-m-d', strtotime('-30 days'));
$a_vencer   = Date('Y-m-d', strtotime('+10 days'));
$vencido    = Date('Y-m-d', strtotime('-1 days'));

$consulta = mysqli_query($conn, "SELECT * FROM customers WHERE status IN (6, 7, 8)");
$linhas = mysqli_num_rows($consulta);

if ($linhas > 0) {
    while ($dados = mysqli_fetch_array($consulta)) {

        $id             = $dados['id'];
        $name_user      = $dados['first_name'];
        $due_date       = $dados['due_date'];
        $status_user    = $dados['status'];

        if ($vencimento == $a_vencer) {

            $atualiza = mysqli_query($conn, "UPDATE customers SET status = 11, change_date = '$hoje' WHERE codigo = '$id_user'");
            if ($atualiza) {
                echo "Atualizado o status do usuário a vencer em 10 dias: $id - $name_user - VENCE EM -> " . date('d/m/Y', strtotime($vencimento)) . "<br>";
            } else {
                echo "Erro ao atualizar vencimento em 10 dias do usuário  - $id - $name_user - EM -> " . date('d/m/Y h:i:s') . "<br>";
            }
        } elseif ($vencimento < $vencido) {

            $atualiza = mysqli_query($conn, "UPDATE customers SET status = 9, change_date = '$hoje' WHERE codigo = '$id_user'");
            if ($atualiza) {
                echo "Atualizado o status do cliente vencido: $id - $name_user - VENCEU EM -> " . date('d/m/Y', strtotime($vencimento)) . "<br>";
            } else {
                echo "Erro ao atualizar o cliente vencido - $id - $name_user - EM -> " . date('d/m/Y h:i:s') . "<br>";
            }
        }
    }
} else {
    echo "Nenhum cliente para atualizar status - " . date('d/m/y h:i:s') . "<br>";
}
  • If you’re falling in the else is because the condition if ($linhas > 0) is not satisfied. Are you sure that you are connecting in the correct database and that there is no error in the query?

  • Yes, because when I run it directly in the browser it does the update. But I believe it is the cron configuration, I’m seeing in the documentation to see how to run php file correctly on the hostgator cron

  • And you are sure that when CRON is run there are records pending for update?

  • Yes there is, because when I give a select in the base, the same select I use in the file, it has the return, but I was able to solve with a setting in the cron call, it was necessary to put php -q before the cron call. Anyway thanks

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.