如何在另一个存储过程(PHP和mysqli)中调用存储过程

如何在另一个存储过程(PHP和mysqli)中调用存储过程,php,mysqli,mysql-workbench,Php,Mysqli,Mysql Workbench,我正在努力调用另一个存储过程中的存储过程。就像现在一样,存储过程永远不会返回SELECT语句作为PHP中mysqli调用的结果,但它在MySQL工作台中运行良好 DELIMITER $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `new_bid`(IN bid_in decimal(6,2), IN ticker_in varchar(5)

我正在努力调用另一个存储过程中的存储过程。就像现在一样,存储过程永远不会返回SELECT语句作为PHP中mysqli调用的结果,但它在MySQL工作台中运行良好

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `new_bid`(IN bid_in decimal(6,2),
                                                      IN ticker_in varchar(5),
                                                      IN share_amount_in BIGINT)
BEGIN

    DECLARE company_id_var INT;
    DECLARE highest_bid BIT;
    DECLARE generated_bid_id BIGINT;

    SET company_id_var = (SELECT ID
                      FROM Companies
                      WHERE Ticker = ticker_in);

    -- Put the bid into the Bids table.
    INSERT INTO Bids(company_id,bid,share_amount)
    VALUES (company_id_var,bid_in,share_amount_in);

    SET generated_bid_id = LAST_INSERT_ID();

    CALL check_available_shares(bid_in,share_amount_in,generated_bid_id,@shares_left);

    -- Check if the bid is higher than the current highest bid.
    -- If so update the CurrentState table to have the new value.
    UPDATE CurrentState SET buyer = bid_in
                        WHERE ID = company_id_var
                        AND buyer < bid_in;

    IF (ROW_COUNT() = 1)
     THEN
     SELECT 1 AS highest_bid, @shares_left AS shares_left, CS.buyer, CS.seller, CS.last_price, CO.name,  CO.ticker
     FROM CurrentState CS, Companies CO
     WHERE CS.id = CO.id
     AND CO.ticker = ticker_in;
    ELSE
     SELECT 0 AS highest_bid, @shares_left AS shares_left, CS.buyer, CS.seller, CS.last_price, CO.name,  CO.ticker
     FROM CurrentState CS, Companies CO
     WHERE CS.id = CO.id
     AND CO.ticker = ticker_in;
    END IF;
END
如果我从此存储过程中删除CALL函数,它将返回底部的一条SELECT语句,但如果我保留CALL函数,它不会返回任何内容。 我尝试过使用exec和execute,但当我试图在MySQL工作台中保存该过程时,这只会导致语法错误。这一问题与环境有关。
但是,这个问题似乎与mysqli如何处理存储过程的执行有关,因为当我在mySQL工作台中手动调用存储过程时,它工作得很好。在几个小时的碰壁之后,我找到了一个解决方案。由于此存储过程调用另一个使用游标的存储过程,因此我必须使用$mysqli->multi_query函数。下面是我如何让它工作的:

新的投标存储过程:

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `new_bid`(IN bid_in decimal(6,2),
                                                      IN ticker_in varchar(5),
                                                      IN share_amount_in BIGINT)
BEGIN

    -- Store the company ID into a variable as this value will used more than once.
    DECLARE company_id_var INT;
    DECLARE ParamtoPass INT;
    DECLARE highest_bid BIT;
    DECLARE generated_bid_id BIGINT;

    SET company_id_var = (SELECT ID
                      FROM Companies
                      WHERE Ticker = ticker_in);

    -- Put the bid into the Bids table.
    INSERT INTO Bids(company_id,bid,share_amount)
    VALUES (company_id_var,bid_in,share_amount_in);

    SET generated_bid_id = LAST_INSERT_ID();
    -- EXECUTE check_available_shares bid_in, @share_amount_in,@generated_bid_id,@shares_left;
    CALL check_available_shares (bid_in,@share_amount_in,generated_bid_id,@shares_left);

    -- Check if the bid is higher than the current highest bid.
    -- If so update the CurrentState table to have the new value.
    UPDATE CurrentState SET buyer = bid_in
                        WHERE ID = company_id_var
                        AND buyer < bid_in;

    IF (ROW_COUNT() = 1)
     THEN
     SELECT 1 AS highest_bid, @shares_left AS shares_left, CS.buyer, CS.seller, CS.last_price, CO.name,  CO.ticker
     FROM CurrentState CS, Companies CO
     WHERE CS.id = CO.id
     AND CO.ticker = ticker_in;
    ELSE
     SELECT 0 AS highest_bid, @shares_left AS shares_left, CS.buyer, CS.seller, CS.last_price, CO.name,  CO.ticker
     FROM CurrentState CS, Companies CO
     WHERE CS.id = CO.id
     AND CO.ticker = ticker_in;
    END IF;
END
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `check_available_shares`(IN bid_in decimal(6,2),
                                                                     IN share_amount_in int,
                                                                     IN bid_id bigint,
                                                                     OUT shares_left BIT)
BEGIN
    DECLARE num_shares INT;
    DECLARE selling_id INT;
    DECLARE selling_price DECIMAL(6,2);
    DECLARE done INT DEFAULT FALSE;

    DECLARE cur CURSOR FOR SELECT share_amount,sell_price,ID
                           FROM ShareSell
                           WHERE sell_price <= bid_in
                           ORDER BY sell_price ASC, sell_timestamp ASC;

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;   /* open cursor */

    read_loop: LOOP
        FETCH NEXT FROM cur
                   INTO num_shares, selling_price, selling_id;

        IF (done) THEN
            LEAVE read_loop;
        END IF;

        IF ((share_amount_in - num_shares) > 0) THEN
            SET share_amount_in = (share_amount_in - num_shares);

            DELETE FROM ShareSell
                   WHERE ID = selling_id;
        ELSEIF((share_amount_in - num_shares) < 0) THEN
            UPDATE ShareSell
            SET share_amount = (share_amount - share_amount_in)
            WHERE ID = selling_id;

            DELETE FROM Bids
            WHERE ID = bid_id;

            LEAVE read_loop;
        ELSE
            DELETE FROM ShareSell
                   WHERE ID = selling_id;

            DELETE FROM Bids
                   WHERE ID = bid_id;
        END IF;

        SELECT num_shares,selling_price;
    END LOOP;

    CLOSE cur;

    IF (share_amount_in > 0) THEN
        UPDATE Bids
        SET share_amount = share_amount_in
        WHERE ID = bid_id;
        SET shares_left = 1;
    ELSE
        SET shares_left = 0;
    END IF;
END

谢谢!这也正是我的问题。我也因为一个完全相似的问题而头疼。你的帖子帮我解决了我的问题

我也遇到过一个PHP调用SP1的例子,SP1反过来又调用SP2,在mysql客户机中一切正常,但被PHP调用时会崩溃!事实上,我在SP2中也选择了一个结果集。我的问题症状与你的完全相同,而且我从来没有想到过使用multi_查询

我注意到您还选择了一个结果集*在检查可用股份中,请参见第行选择数量股份,卖出价格;。不知道你为什么有这条线。如果删除该行,我怀疑您应该能够执行一个查询而不是多个查询

$bid = 43.10;
$ticker = "GOS";
$share_amount = 1000;

if ($mysqli->multi_query("CALL new_bid($bid,'$ticker',$share_amount)"))
{
 if ($result = $mysqli->store_result())
 {
   while ($row = $result->fetch_assoc())
   {
     print_r($row);
   }

   $result->close();
 }
}