如何从存储过程中获取结果,该过程运行正常,但是php返回null

时间:2019-04-25 17:59:49

标签: php mysql stored-procedures mysqli phpmyadmin

我有一个存储过程在我的mysql服务器上运行,代码如下:

DELIMITER $$
CREATE DEFINER=`present`@`%` PROCEDURE `InsertNewEventOrderItems`(IN `UserIDParam` INT, IN `Time` VARCHAR(255), IN `EventIDParam` INT, OUT `Result` VARCHAR(255))
    MODIFIES SQL DATA
BEGIN

    DECLARE OrderIDVar INT DEFAULT 0;

    IF (EXISTS(SELECT * FROM _newapp_shop_orders where UserID=UserIDParam AND Status='Open')) THEN

        SELECT OrderID INTO OrderIDVar FROM _newapp_shop_orders WHERE UserID=UserIDParam AND Status='Open';

        IF (NOT EXISTS(SELECT * FROM _newapp_shop_order_items WHERE OrderID=OrderIDVar AND EventID=EventIDParam)) THEN  

            -- User has open order so create a new order item!

            SELECT OrderID INTO OrderIDVar FROM _newapp_shop_orders WHERE UserID=UserIDParam AND Status='Open';

            INSERT INTO _newapp_shop_order_items (OrderID, EventID, ActivityID, Price, Amount) VALUES (OrderIDVar, EventIDParam, NULL, (SELECT Price FROM _newapp_shop_products_events WHERE EventID=EventIDParam), 1);

            -- SELECT 'InsertOrderItem' INTO Result;  

            SELECT ROW_COUNT() INTO Result;

        ELSE 

            UPDATE _newapp_shop_order_items SET Amount=Amount + 1 WHERE OrderID=OrderIDVar AND EventID=EventIDParam;

            -- SELECT 'UpdateOrderItem' INTO Result;

            SELECT ROW_COUNT() INTO Result;

        END IF;


    ELSE 

        -- User has no open order so create a new order!

        INSERT INTO _newapp_shop_orders (UserID, Created, Modified) VALUES (UserIDParam, Time, Time);

        SELECT OrderID INTO OrderIDVar from _newapp_shop_orders where UserID=UserIDParam AND Status='Open';

        IF (NOT EXISTS(SELECT * FROM _newapp_shop_order_items WHERE OrderID=OrderIDVar AND EventID=EventIDParam)) THEN  

            -- User has made a new order item so create a new order item!

            INSERT INTO _newapp_shop_order_items (OrderID, EventID, ActivityID, Price, Amount) VALUES (OrderIDVar, EventIDParam, NULL, (SELECT Price FROM _newapp_shop_products_events WHERE EventID=EventIDParam), 1);

            -- SELECT 'InsertOrderItem' INTO Result;  

            SELECT ROW_COUNT() INTO Result;  

        ELSE 
            -- User has made an existing order item so update the order item!

            UPDATE _newapp_shop_order_items SET Amount=Amount + 1 WHERE OrderID=OrderIDVar AND EventID = EventIDParam;

            -- SELECT 'UpdateOrderItem' INTO Result;

            SELECT ROW_COUNT() INTO Result;

        END IF;

    END IF;
END$$
DELIMITER ;

它插入一个新订单,订单商品或更新现有商品,它返回修改后的行以检查插入是否成功。当我在phpmyadmin中执行该过程时,它会导致插入的行很好,并将正确的行插入到附属表中。但是,当我从php运行此过程时,它仅返回NULL,这是我的php代码:

public function pushItemToCart($userID, $item)
    {
        $itemList = json_decode($item);
        if ($itemList->Type == 'events') {
            $sql = 'CALL `InsertNewEventOrderItems`(?, ?, ?, @Result)';
        } elseif ($itemList->Type == 'activities') {
            $sql = 'CALL `InsertNewActivityOrderItems`(?, ?, ?, @Result)';
        }

        if ($query = $this->DB->prepare($sql)) {
            $nowFormat = mktime(
                date('H'),
                date('i'),
                date('s'),
                date('m'),
                date('d'),
                date('Y')
            );

            $query->bind_param('isi', $userID, $nowFormat, $itemList->Id);

            $query->execute();

            $query->next_result();

            $select = $this->DB->query('SELECT @Result;');

            //$query->execute();

            $result = $select->fetch_assoc();

            $query->close();

            echo json_encode($result['@Result']);

            exit();
        } else {
            $error = $this->DB->errno . ' ' . $this->DB->error;

            echo json_encode('405'); // 1054 Unknown column 'foo' in 'field list'

            exit();
        }
    }

所以我希望结果是1或2或0

1 个答案:

答案 0 :(得分:0)

我已解决此问题,方法是删除第二个执行程序并完全检查错误程序。更正主要帖子中的代码。