'How to execute PDO outside of loop?

you guys always saying that is it not a good particle to execute SQL queries inside in loops. So how can I run these PDO queries outside of foreach loop?

So first of all I submit a form where I have selected checkboxes.

<input type='checkbox'  name='check_list[]' value='" . $row['c.id'] . "'>

I want to get these data's and submit to API: Name, Phone, City, Adress, Country.

I need to put in foreach loop the $select = $_POST['check_list']; to SELECT each selected checkbox (customer) data (Name, Phone, City, Adress, Country).

foreach ($select as $selected) {

    $stmtgetuserdata = $connpdo->prepare("SELECT c.name, c.phone, 
                                                c.city, c.adress, c.country
                                      FROM `customers` c 
                                      WHERE c.id = :id");
    $stmtgetuserdata->execute([":id"=>$selected]);

    /// FETCH CUSTOMER DATA

    $customerdata  = $stmtgetuserdata->fetch();
    $name          = $customerdata['name'];
    $phone         = $customerdata['phone'];
    $city         = $customerdata['city'];
    $adress        = $customerdata['adress'];
    $country        = $customerdata['country'];

    /// INSERT TO API

    $api = new APIclient(API_EMAIL, API_KEY);

    $api->setClient(array(
            'name' => $name,
            'address' => $adress,
            'city' => $city,
            'phone' => $phone,
            'country_iso_id' => $country
        ));
 
    /// SAVE
    $response = $api->save();

    // IF WAS SUCCESSFUL
    if ($response->error === 0) {
        $stmtupdatesup = $connpdo->prepare("UPDATE customers 
                                     SET status = 'SUCCESSFULLY INSERTED'
                                     WHERE id =:id");
        $stmtupdatesup->execute([":id"=>$selected]);
    } else {
        $stmtupdatesup = $connpdo->prepare("UPDATE customers 
                                            SET status = 'API ERROR' 
                                            WHERE id =:id");
        $stmtupdatesup->execute([":id"=>$selected]);
    }
}

In this example how can I put outside of the foreach loop these queries?

Thank you!

UPDATE:

And what if I have one more loop in this foreach loop for example:

//// GET PRODUCT DATA
$stmtgetproductdata = $connpdo->prepare("SELECT products.productid, 
                                                    orderitems.item_price, 
                                                    products.productname, 
                                                    products.productqty
                                         FROM `orders` 
                                         INNER JOIN orderitems ON orderitems.orderid = orders.orderid 
                                         INNER JOIN products ON products.productid = orderitems.productid 
                                         WHERE orders.customerid =:id");
            
$stmtgetproductdata->execute([":id"=>$selected]);

while ($rowproduct = $stmtgetproductdata->fetch()) {
    $price       = $rowproduct['item_price'];
    $productqty  = $rowproduct['productqty'];
    $productname = $rowproduct['productname'];
    $productid   = $rowproduct['productid'];

    /// ADD ITEMS TO INVOICE
    $api->addItem(array(
        'name' => $productname,
        'quantity' => $productqty,
        'stock_item_id' => $productid,
        'price' => $price,
        'unit' => 'ks',
        'tax' => 20.00
    ));
} // END WHILE 

Is it still okay?



Sources

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

Source: Stack Overflow

Solution Source