在MySQL PDO查询中使用数组变量

时间:2018-03-09 04:54:29

标签: php mysql

我正在尝试使用PDO修复php脚本中的mysql更新。

我有一个数组$ order_ids,它包含正确的记录,然后我使用一个名为$ placeholders的变量来破坏我的选择查询的值,$ detailstatcheck。

所有这些都有效,但我在底部添加的更新无法正常工作,当我运行脚本时,它会打印出更新的0条记录。

我只想说“如果order_id在$ order_ids或$ 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";

更新:

order_status表的结构

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

1 个答案:

答案 0 :(得分:2)

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