使用pdo_sqlsrv执行存储过程
我试图弄清楚如何在sql server 2008上用php5.3/pdo_sqlsrv执行存储过程.
I'm trying to figure out how do execute a stored procedure with php5.3/pdo_sqlsrv on sql server 2008.
我找到了以下代码:
$sql = new PDO( "sqlsrv:server=$server;Database = $database", $user, $password);
$query = "{? = CALL sp_Login(?, ?)}";
$stmt = $sql->prepare( $query );
$returnVariable = 0;
$inputVariable1 = 'input1';
$inputVariable2 = 'input2';
$stmt->bindParam(1,$returnVariable,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT,100);
$stmt->bindParam(2,$inputVariable1,PDO::PARAM_STR);
$stmt->bindParam(3,$inputVariable2,PDO::PARAM_STR);
$stmt->execute();
echo "Return value: ".$returnVariable;
存储过程有两个输入和一个输出参数,但是似乎它什么也不返回,返回值仍然为0.
the stored procedure has two input and one output paramter, but it seems it returns nothing, return value is still 0..
我可以运行选择/插入查询,所以它不是连接.
I can run select/insert queries, so it's not the connection.
是否有关于pdo_sqlsrv存储过程的良好文档?
Is there any good documentation about stored procedures with pdo_sqlsrv?
谢谢!
经过一天的搜索,我发现了一种方法来调用sp ... 问题是存储过程正在运行插入查询,我不得不调用 nextRowset()获取返回值
after a day of search i found a way of calling the sp... the problem was that the stored procedure was running a insert query and i had to call nextRowset() to get the return value
$sql = new PDO( "sqlsrv:server=$server;Database = $database", $user, $password);
$input1 = "input1";
$input2 = "input2";
$return_value = -1;
$proc = '{? = CALL sp_Name (?, ?) }';
$stmt = $sql->prepare( $proc );
$stmt->bindParam(1,$return_value ,PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT,4);
$stmt->bindParam(2,$input1,PDO::PARAM_STR);
$stmt->bindParam(3,$input2,PDO::PARAM_STR);
$stmt->nextRowset(); //skip INSERT result
$result = $stmt->fetch(PDO::FETCH_ASSOC);
/* Display the value of the output parameter */
echo "Return value: ".$return_value.'<br>';