我有一个PHP脚本,我需要变成一个MySQL程序。我无法运行PHP脚本,因为这必须由MySQL触发器执行。因为我不知道如何在所有MySQL中解决这个问题,所以我用PHP编写了它。
下面的PHP脚本需要更改为MySQL过程。 25
和http://www.someurl.com/
是从触发器填充的变量(new.order_id
/ new.store_url
):
$query = "SELECT
oc_order_product.quantity,
oc_order_option.option_value_id,
product.id
FROM oc_order_product
JOIN oc_order_option
ON oc_order_option.order_id = oc_order_product.order_id
JOIN product
ON product.oc_product_id = oc_order_product.model
WHERE oc_order_product.order_id = '25' AND oc_order_product.store_url = 'http://www.someurl.com/'";
$result = $mysqli->query($query);
while($row = $result->fetch_object()){
$query = "UPDATE product_option_value SET quantity = quantity - {$row->quantity} WHERE product_id='{$row->id}' AND option_value_id='{$row->option_value_id}'";
$mysqli->query($query);
}
答案 0 :(得分:1)
Oke,我在阅读互联网后找到了自己的答案,做了一些追踪和错误。
我创建了一个存储过程,我将在更新触发器上执行。
触发器
CREATE TRIGGER `order_gereed` AFTER UPDATE ON `oc_order`
FOR EACH ROW IF new.order_status_id = "15"
THEN
CALL VooraadUpdate(new.order_id, new.store_url);
UPDATE push_voorraad SET actie = '1';
END IF
程序
CREATE DEFINER=`root`@`localhost` PROCEDURE `VooraadUpdate`(IN `orderID` INT(11), IN `storeURL` VARCHAR(255))
NO SQL
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a, b, c INT;
DECLARE cur1 CURSOR FOR SELECT
oc_order_product.quantity,
oc_order_option.option_value_id,
product.id
FROM oc_order_product
JOIN oc_order_option
ON oc_order_option.order_id = oc_order_product.order_id
JOIN product
ON product.oc_product_id = oc_order_product.model
WHERE oc_order_product.order_id = orderID AND oc_order_product.store_url = storeURL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a, b, c;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE product_option_value SET quantity = quantity - a WHERE product_id=c AND option_value_id=b;
END LOOP;
CLOSE cur1;
END