gpt4 book ai didi

PHP,基于循环迭代做mysql更新

转载 作者:可可西里 更新时间:2023-11-01 07:48:01 28 4
gpt4 key购买 nike

我有一个 php 文件可以工作到一定程度,但我需要一些关于循环的帮助来执行 MySQl 插入。

以下执行 SELECT,然后存储 order_status 为“S”的所有记录的订单 ID。这非常有效,打印每个适当的订单 ID。

然后我将那些受影响的 ORder ID 推送到一个数组,以便我可以将它们存储起来用于各种功能。

    //Query for checking all records in order_status
$orderCheck = "
SELECT
order_id,
order_status
FROM order_status
";

$result = mysqli_query($mysqlConn, $orderCheck);
$order_ids = array();


//loop results to gather order IDs and store them
while ($row = mysqli_fetch_array($result))
{
$order_id = $row['order_id'];
if ($row['order_status'] == "S")
{
array_push($order_ids, $order_id);
}
}

//print_r($order_ids);
//This does indeed print the correct order Ids

以下部分需要一些工作,我不太确定在这里做什么。我正在迭代,并且 foreach 我有一个更新的迭代,但我担心更新的语法不正确,我不知道我是否需要在这里做另一个数组。

    //This is where I'm stuck//
//Now I need to iterate through those orderIDs and update is_placement to 1, since it's a bit column, and udpate date_updated to curdate()

for($i=0; $i<count($order_id); $i++) {

$sql = "UPDATE order_status SET is_placement = '1' and date_updated = curdate() WHERE order_id = '$order_id'"; //I don't believe this syntax is correct
}

基本上,我需要为先前存储的 $order_id 数组的每次迭代执行 mysql 更新。

非常感谢任何想法或建议/指导。

更新:

数组的输出:

[287] => 11605809
[288] => 11605817
[289] => 11605825
[290] => 11605863
[291] => 11605869
[292] => 11605870
[293] => 11605875
[294] => 12605471
[295] => 12605643
[296] => 12605715
[297] => 12605778
[298] => 12605817

最佳答案

向 MySQL 询问所有订单,然后手动选择状态为 S 的订单,这毫无意义。

更好:

// Select only orders in status S.
$orderCheck = "SELECT order_id FROM order_status WHERE order_status = 'S';"

$result = mysqli_query($mysqlConn, $orderCheck);
$order_ids = array();

while ($row = mysqli_fetch_array($result)) {
$order_ids[] = $row['order_id'];
}

现在您想更新这些记录。如果你一个接一个地做,你会:

foreach ($order_ids as $order_id) {
$sql = "UPDATE order_status SET is_placement = 1, date_updated = DATE(NOW()) WHERE order_id = '$order_id';";
// EXECUTE
}

更快的方法是(我没有添加引号,假设订单 ID 是数字,但如果它们不是数字,事情会变得有点复杂)将所有订单 ID 放在一起并使用 IN语法:

$bunchOfOrders = implode(',', $order_ids);

$singleQuery = "UPDATE order_status SET is_placement = 1, date_updated = DATE(NOW()) WHERE order_id IN ({$bunchOfOrders})";

但是您也可以在 MySQL 中完成所有这些操作:

$orderUpdate = "UPDATE order_status SET is_placement = 1, date_updated = NOW() WHERE order_status = 'S';"

$result = mysqli_query($mysqlConn, $orderUpdate);

由于上述操作不会更改来自 S 的订单状态,因此如果需要,您还可以运行 $orderCheck 并检索所涉及的订单 ID。

为了确保没有任何意外发生,我还将整个操作包装在一个事务中。

另一个转折

如果我们必须根据某些条件(当然是互斥的)运行一系列不同的修改怎么办?

我们可以用 map 来做到这一点,只要我们使用确定性函数并且没有修改改变下面的条件(所以,你不能改变任何“食谱”中的 order_status,否则它会影响后面的那些):

$todo = array(
"order_status='S'" => "is_placement = 1, date_updated = DATE(NOW())",
"order_status='K'" => "is_placement = 2, date_updated = DATE(NOW())",
"order_status='L'" => "owner='NOBODY'",
"o_s='X' AND a=12" => "note='A random condition'",
// We can also supply a "what to do if none of the above":
"DEFAULT" => "note='THIS RECORD DOES NOT MATCH'",
);
$else = array();
foreach ($todo as $when => $then) {
if ('DEFAULT' !== $when) {
$else[] = "({$when})";
} else {
// "ELSE" means when no other condition is matched.
// i.e. NOT when1 AND NOT when2 AND NOT when3...
// which is equivalent to NOT ( when1 OR when2 ... )
$when = "NOT (" . implode(' OR ', $else) . ")";
}
$sql = "UPDATE order_status SET {$then} WHERE {$when};";
// Execute $sql.
}

关于PHP,基于循环迭代做mysql更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49036885/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com