Question

I am using some stored procedures against a datastore shared by more than one application and need to call a stored procedure that has an INOUT parameter.

From documentation this would work something like

$prob_param = null;
$stmt = $pdo->prepare('CALL obnoxiousStoredProc(:problem_param)');
$stmt->bindParam(':problem_param', $prob_param, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 11);
$stmt->execute();
echo 'Prob param: ' . var_export($prob_param, true);

And we would see something like

Prob param: int(420)

BUT There is a bug that prevents this from working as expected See this answer.

So instead we get

Syntax error or access violation: 1414 OUT or INOUT argument $parameter_number for routine $procedure_name is not a variable or NEW pseudo-variable

The workaround, as described in the answer is two-fold, separate your input and output parameters (which I can get my DBA to do) and use local variables

e.g.

$stmt = $pdo->prepare('CALL obnoxiousStoredProc(@problem_param)');

and then use a second query

SELECT @problem_param

To get your value.

My question is: what is the scope of this "local variable" in PDO? Am I setting myself for a race condition if my php code is getting simultaneously?

Was it helpful?

Solution

User-defined variables are connection-specific.

See the documentation.

So no, there is no possibility of a race condition in a php execution context.

OTHER TIPS

This solution worked for me:

$pdo = new PDO(...);
$stmt = $pdo->prepare('CALL obnoxiousStoredProc(@problem_param)');
$stmt->execute();

$stmt2 = $pdo->prepare('SELECT @problem_param');
$stmt2->execute();
$var = $stmt->fetch();

var_dump($var)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top