duane2364 2014-06-05 20:55
浏览 32
已采纳

将PHP脚本转换为MySQL过程

I have a script in PHP that I need to turn into a MySQL procedure. I can't run the PHP script because this must be executed by a MySQL trigger. Because I have no idea how to fix this in all MySQL, I wrote it in PHP.

The PHP script below needs to be changed to a MySQL procedure. The 25 and http://www.someurl.com/ are variables that filled from the trigger (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);
}
  • 写回答

1条回答 默认 最新

  • douyu5679 2014-06-10 08:51
    关注

    Oke, I found my own answer after reading the internet en doing some trail and error.

    I created a stored procedure which I will execute on the update trigger.

    The trigger

    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
    

    The procedure

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?