This is really derivative of my earlier question today.
I created a Stored Procedure in my database that I wanted to call several times in a row from PHP.
Let's say this is my procedure:
CREATE PROCEDURE PROC_1(
IN param1 VARCHAR(255),
IN param2 VARCHAR(255))
BEGIN
DECLARE ok INT;
DECLARE success, failure VARCHAR(255);
/* several SELECT, IF ... THEN, INSERT and UPDATE operations which then SET ok var to 0 or 1 */
IF ok = 1 THEN
SET success = 'Everything went well';
SELECT success;
LEAVE;
ELSE
SET failure = 'Problem description';
SELECT failure;
LEAVE;
END IF;
END
I did it this way (short version):
$calls = array(
"CALL PROC_1('param1', 'param2')",
"CALL PROC_1('param3', 'param4')",
"CALL PROC_1('param5', 'param6')",
);
// assuming I'm already connected to DB with $link
foreach ($calls as $i => $call)
{
echo $i . ': ';
$result = mysql_query($call);
$ok = ($result === FALSE) ? FALSE : TRUE;
var_dump($ok);
if ($result !== FALSE)
mysql_free_result($result);
}
The first iteration works as expected, but anything after returns FALSE
.
Why is that?
Tried mysqli
just in case, but getting exactly same output:
0: bool(true)
1: bool(false)
2: bool(false)
What's interesting, I've checked MySQL logs (logging set to log all queries) and only first query ever gets to the server. Next queries never get to the server.
PS. I'm running PHP 5.3.2 and Apache 2.2.17.
UPDATE
As per Shakti Singh's suggestion I've checked $link
state before querying the database. I noticed there's an error since the second iteration so here's the output with the error:
ERROR: (0)
0: bool(true)
ERROR: (0)
ERROR: (0)
1: bool(false)
ERROR: (2014) Commands out of sync; you can't run this command now
ERROR: (2014) Commands out of sync; you can't run this command now
2: bool(false)
ERROR: (2014) Commands out of sync; you can't run this command now
Also, this appears in MySQL error log:
101126 15:46:28 [Warning] Aborted connection 129 to db: 'db1' user: 'root' host: 'localhost' (Got an error reading communication packets)