I am struggling to call a stored procedure within another stored procedure. Like it is now the stored procedure never return the SELECT statement back as a result to the mysqli call in PHP (but it works fine in MySQL workbench).
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
If I remove the "CALL" function from this stored procedure it returns one of the SELECT statement at the bottom, but if I keep the "CALL" function it does not return anything. I have tried with exec and execute but that only gives me syntax error when I try to save the procedure within MySQL workbench. This problem is related to https://stackoverflow.com/questions/23193085/mysqli-does-not-return-any-results-but-stored-procedure-does. But it seems that that problem may be related to how mysqli handles the execution of the stored procedure since it works fine when I call the stored procedure manually in mySQL workbench(?)
After hours of banging my head against a wall I found a solution. Since this stored procedure calls another stored procedure which uses a cursor I had to use the
$mysqli->multi_query()
function. Here's how I got it working:new_bid stored procedure:
check_available_shares procedure:
And finally the PHP call:
Thanks! This was exactly my problem too. I too broke my head on an exact similar problem (see PHP mysqli is NOT trapping some errors when calling stored procedure). Your post helped me solve my problem!
I too had a case of PHP calling SP1, which in turn called SP2, and everything working fine in a mysql client but breaking when called by PHP! And indeed, I too had had a SELECT of a result set in SP2. And my problem symptoms were exactly identical to yours, and it never struck me to use
multi_query()
.I notice that you also SELECT a "result set* in
check_available_shares
(see lineSELECT num_shares,selling_price;
). Not sure why you have this line. If you remove that line, I suspect you should be able to do aquery()
instead ofmulti_query()
.