Xiaolong
Xiaolong

Reputation: 21

Parameters with PDO for stored procedure

How to call a SQL Server's stored procedure with 2 parameters in PDO.

For example in SQLServer's prompt:

EXEC Test 'arg1', 'arg2'

What is the next step in PHP with PDO?

Thanks in advance for your response.

Upvotes: 2

Views: 1511

Answers (2)

Tobias
Tobias

Reputation: 219

To use muliple parameters, just use bindParam for each parameter, either with named parameters:

$query="EXEC test :arg1, :arg2";
$stmt->bindParam(":arg1",$first_variable);
$stmt->bindParam(":arg2", $second_variable);

$stmt->prepare($query);
$stmt->execute();

Another way is to directly hand the parameters to the execute()-method as an array:

$stmt->execute(array(":arg1"=>$first_variable,":arg2"=>$second_variable

You can also use question marks for all parameters in the query:

$query="EXEC test ?,?";
...
...
$stmt->bindParam(1, $first_variable);
$stmt->bindParam(2, $second_variable);

or directly inside the execute():

$stmt->execute(array($first_variable, $second_variable));

Upvotes: 1

Typhomism
Typhomism

Reputation: 284

I typically use question marks for parameters in PDO, I.E.

$stmt = $db->prepare("UPDATE table SET name=? WHERE id=?");
$stmt->execute(array($name, $id));
$affected_rows = $stmt->rowCount();

You can read a great explanation of PDO query structures here

Upvotes: 0

Related Questions