如何从存储过程中获取结果,过程运行良好但 php 返回 null

Posted

技术标签:

【中文标题】如何从存储过程中获取结果,过程运行良好但 php 返回 null【英文标题】:How to get result from stored procedure, the procedure runs fine but php returns null 【发布时间】:2019-09-15 05:36:30 【问题描述】:

我有一个存储过程在我的 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

【问题讨论】:

你需要更多 >next_result();来电 您可能应该包含该 sql 的实际 CREATE PROCEDURE 部分。 @Uueerdo 更新了我的问题。 @YourCommonSense 在代码中的什么位置? 【参考方案1】:

我已经通过删除第二次执行并全面检查错误过程来解决它。主帖中的正确代码。

【讨论】:

以上是关于如何从存储过程中获取结果,过程运行良好但 php 返回 null的主要内容,如果未能解决你的问题,请参考以下文章

PHP如何调用SQLServer2012的存储过程并获取返回结果集及出参?

如何从存储过程中获取输出参数结果和查询结果?

从 PHP 中的存储过程获取输出

如何从 python 在 mysql 中创建存储过程?

如何从实体框架中的存储过程中获取结果+数据库优先

从过程中获取表输出