Before this is closed as a duplicate out of hand, let me say that there are a few questions that ask this on Stack Overflow, but none of them have marked answers, and the unmarked ones didn't work for me.
In my MySql database, I have a simple procedure:
DELIMITER //
CREATE PROCEDURE foo (
IN a INT,
OUT b INT
) BEGIN
SELECT a INTO b;
END //
DELIMITER ;
If I call this in phpMyAdmin, I get what I expect:
SELECT 2 INTO @b;
CALL foo(1, @b);
SELECT @b;
| @b |
+----+
| 1 |
When I try to call this from my php code, however, stmt->execute()
returns false
.
$a = 1;
$pdoLink = get_pdo_connection($db);
$stmt = $pdoLink->prepare('CALL `mydb`.`foo`(:a, :b)');
$stmt->bindParam(':a', $a);
$stmt->bindParam(':b', $b, PDO::PARAM_INT, 4);
// I've also tried length values of 1, 2, and 16
$stmt->execute(); // returns false
I thought maybe it had to do with calling a procedure in the first place, so I replaced my procedure with:
DELIMITER //
CREATE PROCEDURE foo (
IN a INT,
IN b INT
) BEGIN
SELECT a, b;
END //
DELIMITER ;
This, however, does work from php, so it must be something having to do with output parameters. I'm using code that's only slightly altered from the PDO Documentation, so I really have no idea what's wrong with it.
I am not looking for an answer that uses mysqli's multi_query
to do something like
$sql = "CALL `mydb`.`foo`(1, @b);
SELECT @b;";
$link->multi_query($sql);
// ...
I'm looking for how to do this using PDO and a single query.
Someone tried to mark this as a duplicate of Calling stored procedure with Out parameter using PDO. The marked answer to this question uses multiple queries.