简体   繁体   中英

Using array variable in MySQL PDO query

I'm trying to fix a mysql update within a php script using PDO.

I have an array, $order_ids, that contains the correct records and I then use a variable called $placeholders to implode those values for my select query, $detailstatcheck.

All of this works but the update I added at the bottom is not working, when I run the script it prints that 0 records are updated.

I just want to say "update record if order_id is in $order_ids or $placeholders"

$order_ids = [];
while ($row = $ordStat->fetch(PDO::FETCH_ASSOC)) {
    $order_ids[] = $row['order_id'];
}

if (count($order_ids) > 0) {

$placeholders = implode(',', array_fill(0, count($order_ids), '?'));
//This query works
$detailStatCheck = "
    SELECT 
         invnoc as INVOICE,
         fstatc as STATUS,
         cstnoc AS DEALER,
         framec AS FRAME,
         covr1c AS COVER,
         colr1c AS COLOR ,
         extd2d AS SHIPDATE,
         orqtyc AS QUANTITY
    FROM GPORPCFL
    WHERE invnoc IN ($placeholders)
";

try {
    $detailCheck = $DB2conn->prepare($detailStatCheck);
    $detailRslt = $detailCheck->execute($order_ids);
    $count2 = $detailCheck->fetch();
    print_r($order_ids);
    print_r($count2);
} catch(PDOException $ex) {
    echo "QUERY FAILED!: " .$ex->getMessage();
}

//this update doesn't work
$updateShipped = "
    UPDATE order_status
    set date_updated = current_date()
    where order_id in ($placeholders) //Not sure if syntax is correct here
";


try{
            $updateStatus = $MysqlConn->prepare($updateShipped);
            $statUpdateRslt = $updateStatus->execute();
            $count = $updateStatus->rowcount();
            }
            catch(PDOException $ex)
            {
                echo "QUERY FAILED!: " .$ex->getMessage();
            }
        echo "Records Updated: " . $count . "\n";

UPDATE:

Structure of order_status table

order_id  |  order_status  |  is_placement  |  date_updated
-----------------------------------------------------------
12345           S                  1            2018-03-09

order_id = int(11)

order_status = varchar

is_placement = bit

date_updated = DATE

您在执行查询时缺少$ order_ids参数:$ statUpdateRslt = $ updateStatus-> execute($ order_ids);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM