Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

php - Executing a Stored Procedure with pdo_sqlsrv

I'm trying to figure out how do execute a stored procedure with php5.3/pdo_sqlsrv on sql server 2008.

I've found this code:

$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;

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.

Is there any good documentation about stored procedures with pdo_sqlsrv?

thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

http://social.msdn.microsoft.com/Forums/en/sqldriverforphp/thread/55f21fc5-dbc0-4fe4-a4ae-f15905a4293a

$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>';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...