This one has got me foxed, and I'm hoping the solution is a "you know why that isn't working, don't you?" type of reply.
Here goes: Via PHP I am calling a MySQL stored procedure thus:
$sql = "CALL sp_test;";
$mysqli->query($sql);
No problem there. I know the Stored Procedure gets executed.
Here's the Stored Procedure that I have striped for this example, and it is still producing the same problem.
DELIMITER \\
DROP PROCEDURE IF EXISTS sp_test\\
CREATE PROCEDURE sp_test()
DETERMINISTIC
BEGIN
DECLARE exit_loop TINYINT(1) DEFAULT 0;
DECLARE v_TableName VARCHAR(50); DECLARE v_ScanID INT(11); DECLARE v_ScanType TINYINT(4);
DECLARE v_TitleID INT(11); DECLARE v_VolumeNo INT(11); DECLARE v_IssueNo VARCHAR(10); DECLARE v_IssueDate INT(11); DECLARE v_FIDateUnknown TINYINT(4); DECLARE v_PureHavocScan TINYINT(4);
-- declare cursor for employee email
DECLARE storeroom_cursor CURSOR FOR SELECT TableName, ScanID, ScanType, FIDateUnknown, TitleID, VolumeNo, REPLACE(IssueNo, "\'", "") AS IssueNo, IssueDate FROM t_storeroom ts JOIN t_issueguide tig ON ts.TitleID = tig.ComicTitleID;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;
OPEN storeroom_cursor;
get_purehavoc: LOOP
FETCH storeroom_cursor INTO v_TableName, v_ScanID, v_ScanType, v_FIDateUnknown, v_TitleID, v_VolumeNo, v_IssueNo, v_IssueDate;
IF exit_loop = TRUE THEN
LEAVE get_purehavoc;
END IF;
CASE v_ScanType
WHEN '1' AND v_FIDateUnknown = 0 THEN
SET @sql_text = CONCAT("SELECT @RowCount := COUNT(*) FROM ", v_TableName, " WHERE ComicTitleID = ", v_TitleID, " AND VolumeNo = ", v_VolumeNo, " AND IssueNo = '", v_IssueNo, "' AND IssueDate = ", v_IssueDate, " AND DiggerExclusive = 1;");
WHEN '1' AND v_FIDateUnknown = 1 THEN
SET @sql_text = CONCAT("SELECT @RowCount := COUNT(*) FROM ", v_TableName, " WHERE ComicTitleID = ", v_TitleID, " AND VolumeNo = ", v_VolumeNo, " AND IssueNo = '", v_IssueNo, "' AND DiggerExclusive = 1;");
-- WHEN '2' THEN
-- Do something
END CASE;
PREPARE statement FROM @sql_text;
EXECUTE statement;
-- Setting the PureHavocScan variable to enter into the PREPARE STATEMENT.
IF @RowCount = 0 THEN
SET @PureHavocScan = 1;
ELSE
SET @PureHavocScan = 0;
END IF;
SET @ScanID = v_ScanID;
SET @TableName = v_TableName;
-- SET @sql_text = concat("UPDATE t_storeroom SET PureHavocScan = ? WHERE TableName = ? AND ScanID = ? ");
SET @sql_text = concat("UPDATE t_storeroom SET PureHavocScan = ", @PureHavocScan, " WHERE TableName = '", @TableName, "' AND ScanID = ", @ScanID, ";");
PREPARE statement FROM @sql_text;
EXECUTE statement;
END LOOP get_purehavoc;
DEALLOCATE PREPARE stmt;
CLOSE storeroom_cursor;
END//
DELIMITER ;
When I run this exact Stored Procedure within my MySQL database admin tool (Navicat in my case), the stored procedure works 100% as intended and ALL the rows that should get updated DO get updated.
However, when I run this exact Stored Procedure via PHP, ONLY 1 (sometimes 2) rows get updated.
Other stored procedures that get called in exactly the same way work fine. It's just this one. The ONLY difference with the one that's failing, is that the MySQL command is an UPDATE, as opposed to the SELECTS and INSERTS that work fine.
I've been at this for two days now and have exhausted all 'my' knowledge. So if someone can put me out of my misery, I'd be mightily grateful.