PHP version: 7.4.3
MySQL version: 8.0.42
I have a simple stored procedure which selects a single column from a table, which is called inside a loop from my PHP. The first iteration works fine. The second iteration fails. I have logged the SQL statements which my PHP builds, and when I run the statement for the second iteration in MySQL Workbench, it works as expected.
This is the stored procedure in question:
DELIMITER $$
USE `Valor`$$
CREATE DEFINER=`sean`@`%` PROCEDURE `Critter_GetType`(IN crtr VARCHAR(35))
BEGIN
SELECT `type` FROM Valor.CreatureStats WHERE creature = crtr LIMIT 1 ;
END$$
DELIMITER ;
;
This is my PHP code calling it:
$sql = "CALL Valor.Critter_GetType('$cc');";
error_log("sql = $sql");
$typeresult = NULL;
$typeresult = $conn->query($sql);
error_log("num rows = $typeresult->num_rows");
while($typerow = $typeresult->fetch_assoc()){
$type = $typerow['type'];
}
I set $typeresult = NULL
because that was a suggestion I found for a similar problem, but it doesn’t help. The logged SQL works as expected when I call it from outside of the PHP script. The error stops the script, so when I say that the second iteration fails, I’m not implying that there is a third iteration which runs OK.
The first sign that something is wrong is the message
Trying to get property ‘num_rows’ of non-object” in the line “error_log(“num rows = $typeresult->num_rows”)”.
This message occurs, whether or not I have the $typeresult = NULL;
line. Then, of course, the “while” loop is where the script-killing error occurs.
This is the first time I’ve used a stored procedure with PHP. I don’t know if that’s relevant, but I have dozens of other SQL calls from inside loops in my PHP which do not have this problem.