【问题标题】:Accessing Returned value from MySQL Stored Procedure in PHP在 PHP 中访问 MySQL 存储过程的返回值
【发布时间】:2019-10-10 12:01:37
【问题描述】:

我有这个存储过程,它有 2 个参数,第一个是 cartId(它是 IN),我有 newOrderId(它是 OUT)。现在, 我知道 inCartId 是购物车 ID,它将来自表单或购物车被访问的任何方式,但我不明白 OUT newOrderId 因为这是 MySQL 将返回的。 现在,在调用这个存储过程的 PHP 中,它会是这样的。

$sql = 'CALL create_order(:cart_id, "What will be here")// The cart_id is what PHP will pass to MySQL, but what will be passed
    to the 2nd argument since PHP does not know before hand what MySQL will return.
CREATE PROCEDURE create_order(
    IN inCartId int,
    OUT newOrderId int
)
BEGIN

    -- Insert a new record into orders and obtain the new order ID
    INSERT INTO orders (created_on) VALUES (NOW());

    -- Obtain the new Order ID
    SELECT LAST_INSERT_ID() INTO newOrderId;

    -- Insert order details in order_detail table
    INSERT INTO order_detail (
        order_id, product_id, attributes, product_name, quantity, unit_cost
    )
    SELECT
        orderId,
        p.id,
        sc.attributes,
        p.name,
        sc.quantity,
        COALESCE( NULLIF( p.discounted_price, 0 ), p.price ) AS unit_cost
    FROM
        shopping_cart sc
        INNER JOIN products p ON sc.product_id = p.id
    WHERE
        sc.cart_id = inCartId
        AND
        sc.buy_now;

    -- Save the order's total amount
    UPDATE
        orders
    SET
        total_amount = (
            SELECT
                SUM( unit_cost * quantity )
            FROM
                order_detail
            WHERE
                order_id = orderId
        )
    WHERE
        id = orderId;

    -- Clear the shopping cart
    CALL shopping_cart_empty(inCartId);

END```

【问题讨论】:

    标签: php mysql stored-procedures


    【解决方案1】:

    已经解决了。我就是这样做的。

    CREATE PROCEDURE `shopping_cart_create_order2`(IN `inCartId` INT(11), OUT `newOrderId` INT(11)) BEGIN
        DECLARE newOrder int;
        -- Insert a new record into orders and obtain the new order ID
        INSERT INTO orders (created_on) VALUES (NOW());
    
        -- Obtain the new Order ID
        SELECT LAST_INSERT_ID() INTO newOrder;
    
        -- Insert order details in order_detail table
        INSERT INTO order_detail (
            order_id, product_id, attributes, product_name, quantity, unit_cost
        )
        SELECT
            newOrder,
            p.id,
            sc.attributes,
            p.name,
            sc.quantity,
            COALESCE( NULLIF( p.discounted_price, 0 ), p.price ) AS unit_cost
        FROM
            shopping_cart sc
            INNER JOIN products p ON sc.product_id = p.id
        WHERE
            sc.cart_id = inCartId
            AND
            sc.buy_now;
    
        -- Save the order's total amount
        UPDATE
         orders
        SET
            total_amount = (
                SELECT
                    SUM( unit_cost * quantity )
                FROM
                    order_detail
                WHERE
                    order_id = newOrder
            )
        WHERE
            id = newOrder;
    
        -- Clear the shopping cart
        CALL shopping_cart_empty(inCartId);
        SET newOrder = newOrderId;
    END
    

    在 PHP 级别// 可能在模型/实体级别 首先,我们需要执行

    shopping_cart_create_order2()

    存储过程。这可能在一个函数中。

    其次,要获取最后的订单id,需要从变量中查询

    @oid

    。重要的是我们必须调用方法

    closeCursor()

    PDOStatement 对象,以便执行下一条 SQL 语句。

    function query($pdo, $sql, $parameters = []){
        $query = $pdo->prepare($sql);
        $query->execute($parameters);
        return $query;
    }
    
    function create_order($pdo, $cart_id){
        // Binding the parameters
        $parameters = [':cart_id' => $cart_id];
    
        // calling stored procedure command
        $sql = 'CALL shopping_cart_create_order2(:cart_id)';
    
        // prepare for execution of the stored procedure, pass value to the command 
        and execute the Stored Procedure
        $query = query($pdo, $sql, $parameters);
    
       // Then close Cursor. It is important for you to close it.
        $query->closeCursor();
    
       // execute the second query to get last insert id
        $row = $pdo->query("SELECT @oid AS oid")->fetch();
        return $row;    
    }
    

    欲了解更多信息,请参阅enter link description here

    我希望能够帮助那些可能陷入我所经历的事情的人。

    【讨论】:

      猜你喜欢
      • 2015-11-09
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 2013-03-18
      • 1970-01-01
      相关资源
      最近更新 更多