php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50555 PDO_DBLIB: Cannot retrieve output parameter from stored procedure
Submitted: 2009-12-22 22:09 UTC Modified: 2017-09-07 21:50 UTC
Votes:33
Avg. Score:4.8 ± 0.5
Reproduced:32 of 32 (100.0%)
Same Version:4 (12.5%)
Same OS:5 (15.6%)
From: david dot wright at opticsplanet dot com Assigned:
Status: Open Package: PDO DBlib
PHP Version: 5.3.1 OS: 2.6.24-24-server
Private report: No CVE-ID: None
 [2009-12-22 22:09 UTC] david dot wright at opticsplanet dot com
Description:
------------
I cannot retrieve an output parameter from a stored procedure (in my case on SQL Server 2005--am using PDO_DBLIB.

Reproduce code:
---------------
PHP Code:
---------------------------------------------------
/** SNIP. Set up a valid $db here! **/
$return_value = 999;
$sth = $db->prepare("EXEC dbo.opsp_Test ?");
$sth->bindParam(1, $return_value, PDO::PARAM_STR |
PDO::PARAM_INPUT_OUTPUT, 4);
$sth->execute();
echo "$return_value\n";

Stored Procedure
--------------------------------------------------
CREATE PROCEDURE opsp_Test 
	@TheOutputValue int OUTPUT
AS
BEGIN
	SET @TheOutputValue = 11
END



Expected result:
----------------
output should be: 11

Actual result:
--------------
Output is 999 ($return_value unchanged)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-09 10:50 UTC] a at exampl dot com
For fucks sake would anybody already fix this? It's not the only report about this issue in the tracker.
 [2010-09-18 21:34 UTC] ssufficool@php.net
This requires that pdo_dblib pass params using the RPC mechanisms and also to implement it's own driver level binding instead of relying on the PDO param binding. This will require a rewrite of most of the statement object.

Rest assured, it is being worked on but may take some time.
 [2013-06-12 04:05 UTC] ssufficool@php.net
-Summary: Cannot retrieve output parameter from stored procedure +Summary: PDO_DBLIB: Cannot retrieve output parameter from stored procedure
 [2014-01-01 12:49 UTC] felipe@php.net
-Package: PDO related +Package: PDO DBlib
 [2014-05-17 09:05 UTC] eugene at bcp dot im
This bug/problem whatever you want to call it is not limkted to DBlib - happening on ODBC and SqlServer... Is anyone looking into this??
 [2016-02-12 13:14 UTC] g at thelettereyediversity dot co dot za
Working with the latest PHP 5.6 and MySQL 5.6.22 and can confirm this is still a bug and affects the PDO library.
 [2016-02-16 11:12 UTC] craig_carnell at hotmail dot com
I am experiencing the same issue with PHP 7.0.3, pdo_dblib and MSSQL 2008. Unfortunately I am having to resort to pdo_dblib due to the removal of mssql* functions.

Here is my code:

$db = new PDO('dblib:host=myhost:1433;dbname=mydb', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "EXEC Stock_Level @myId=:myId, @StockLevel=:StockLevel";
$stmt = $db->prepare($sql);
$var = '12345';
$var2 = null;
$stmt->bindParam(":myId", $var, PDO::PARAM_STR);
$stmt->bindParam(":StockLevel", $var2, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT);

if(!$stmt->execute()) {
    echo "\nPDO::errorCode(): ", $stmt->errorCode();
}

echo "var 2 = " . $var2 . PHP_EOL;

This is a big problem!
 [2016-09-12 22:01 UTC] adambaratz@php.net
Alternate test case in #70274.
 [2017-09-06 08:31 UTC] php dot net at itsacon dot net
This bug still persists in PHP 7.1.9

And since PHP 7.x has dropped support for the old mssql extension, this is now a very important bug, since it means there is no way to connect a Linux/BSD server to a Microsoft SQL server and have full stored procedure functionality.

I have tried both PDO_ODBC and PDO_DBLIB, different versions of FreeTDS (both with iODBC and UnixODBC backends), and I've found nothing that works for stored procedures with OUTPUT parameters.
 [2017-09-07 21:50 UTC] adambaratz@php.net
You should be able to write a query where you:
 - DECLARE a var
 - run the sproc, using the var as the output parameter
 - SELECT the var

Not as elegant as being able to use bindParam, but it's a workaround.
 [2017-10-06 07:34 UTC] php dot net at itsacon dot net
That might work, but only if you're willing to rewrite an entire application with an ugly hack in order to upgrade to PHP 7.x

I really hope this gets fixed before 5.6 End-Of-Life.
 [2018-11-20 13:41 UTC] jeremy dot hise at gmail dot com
Ubuntu, PHP 7, mySQL, still a problem. I'm sure other stacks (.NET/SQL Server/Java/Oracle) don't have this problem. This is now been around for almost 10 years? 

Please please, fix this.
 [2019-02-08 08:03 UTC] php dot net at itsacon dot net
...and PHP 5 has gone end-of-life with still no updates on this bug...

Which means that everyone using Microsoft SQL server on a *n*x system is either running an unsupported version, or unable to use most of the functionality of his database.
 [2022-02-23 06:50 UTC] karannegi0321 at gmail dot com
Nice article, Thanks for sharing..!!
 [2023-06-13 13:04 UTC] diogotbrito at gmail dot com
DBLIB on unix cant accesses variables. Changing to windows or sqlsrv with the same code, works. I have a big client running procedure and i need to ge the return LOL

<?php

$database = "";
$server = "";
$user = "";
$pass="";


$str_host  = "dblib:host=" . $server;
$str_host .= ";dbname=" . $database;

try {
    $con = new PDO($str_host, $user, $pass);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $con->exec("SET NOCOUNT ON");
} catch (Exception $ex) {
    print_r('Error: ' . $ex->getMessage());
    exit;
}

$comando = "DECLARE @mensagem varchar(20); SET @mensagem = 'Diogo Toscano'; SELECT @mensagem;";
$sth = $con->query($comando);
foreach ($sth as $row) {
    echo "<pre>";
    print_r ($row);
}
 [2023-06-13 17:57 UTC] diogotbrito at gmail dot com
Expected result:
----------------
Array
(
    [] => Diogo Toscano
    [0] => Diogo Toscano
)


Actual result:
--------------
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC