'PDO Query inside multiple loops, is that okay?

I have a code which is actually a CRON job. The task is to SELECT the new customers and check if that customer is returning customer or actually a new customer. I have to note that we dont save any email adresses and we dont have a register (sign up) form. If someone orders from us then we every time create a new "account" for that customer. I dont know if its makes sense for you.

So I have a NULL column (returned) on my table a null value will mean that the cron job didn't yet searched for that customer.

So here is my code:

/// GET ALL THE NEW ORDERS WHERE IS "RETURNED" NULL == NOT CHECKED YET
$stmtgetpending = $connpdo->query("SELECT name, mobile 
                                   FROM customers
                                   WHERE returned IS NULL 
                                   AND DATE(o_date) BETWEEN (NOW() - INTERVAL 14 DAY) AND (NOW());");

$stmtgetpending->execute();
$totalrowpending = $stmtgetpending->rowCount();
/// SELECT THE ID FROM CUSTOMERS WHERE ARE SIMILIAR NAME AND PHONE
$searchquery     = $connpdo->prepare("SELECT id
                                      FROM customers 
                                      WHERE name LIKE :name 
                                      AND mobile LIKE :phone 
                                      AND returned IS NULL
                                      ORDER BY id");
/// IF THERE ARE NEW ORDERS (RETURNED == NULL/NOT CHECKED)
if ($totalrowpending > 0) {
    $pendings = $stmtgetpending->fetchAll();
    try {
        
        /// UPDATE THE CUSTOMER WHEN THERE ARE RESULT QUERY   
        $stmtupdatecustomers = $connpdo->prepare("UPDATE 
                                          customers cus 
                                          SET cus.returned = '1' 
                                          WHERE cus.id = :id");
        
        /// UPDATE THE CUSTOMER WHEN THERE IS NOT RESULT QUERY                                        
        $stmtupdatecustomerszero = $connpdo->prepare("UPDATE 
                                              customers cus 
                                              SET cus.returned = '0' 
                                              WHERE cus.id = :id");
        
        $connpdo->beginTransaction();
        
        /// FOREACH THE NEW ORDERS
        foreach ($pendings as $row) {
            $name  = $row["name"];
            $phone = $row["mobile"];
            /// SEARCH BY THE NAME AND PHONE
            $searchquery->execute([':name' => "%$name%",
                                   ':phone' => "%$phone%"]);
            /// COUNT IF THERE ANY RESULT
            $totalrowuserdata = $searchquery->rowCount();
            if ($totalrowuserdata >= 2) {
                /// IF THERE ARE RESULT UPDATE THE RETURNED COLUMN TO 1
                $searchresults = $searchquery->fetchAll();
                foreach ($searchresults as $rowsearch) {
                    $stmtupdatecustomers->execute([':id' => $rowsearch["id"]);
                }
            }
            /// IF THERE ARE NOT RESULT UPDATE THE RETURNED COLUMN TO 0
            else {
                $searchresults = $searchquery->fetchAll();
                foreach ($searchresults as $rowsearch) {
                    $stmtupdatecustomerszero->execute([':id' => $rowsearch["id"]);
                }
            }
        }
        $connpdo->commit();
    }
    catch (PDOException $e) {
        $connpdo->rollBack();
        
        error_log("Error occurred. Error message: " . $e->getMessage(), 0);
    }
    
} else {
    echo "No new orders for now";
}

So the question. Is this okay for put into cron job?

Thanks for your help!

Have a nice day!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source