PHP: Calling MySQL Stored Procedure with Both INPUT AND OUTPUT Parameters (NOT "INOUT")
Unfortunately, MySQLi does not have any native support for output sproc parameters; one must instead output into MySQL user variables and then fetch the values using a separate SELECT
statement.
Using the procedural interface:
$procInput1 = 123;
$procInput2 = 456;
$procInput3 = 789;
$mysqli = mysqli_connect();
$call = mysqli_prepare($mysqli, 'CALL test_proc(?, ?, ?, @sum, @product, @average)');
mysqli_stmt_bind_param($call, 'iii', $procInput1, $procInput2, $procInput3);
mysqli_stmt_execute($call);
$select = mysqli_query($mysqli, 'SELECT @sum, @product, @average');
$result = mysqli_fetch_assoc($select);
$procOutput_sum = $result['@sum'];
$procOutput_product = $result['@product'];
$procOutput_average = $result['@average'];
Or, using the object-oriented interface:
$procInput1 = 123;
$procInput2 = 456;
$procInput3 = 789;
$mysqli = new mysqli();
$call = $mysqli->prepare('CALL test_proc(?, ?, ?, @sum, @product, @average)');
$call->bind_param('iii', $procInput1, $procInput2, $procInput3);
$call->execute();
$select = $mysqli->query('SELECT @sum, @product, @average');
$result = $select->fetch_assoc();
$procOutput_sum = $result['@sum'];
$procOutput_product = $result['@product'];
$procOutput_average = $result['@average'];