I've a procedure which takes multiple params as input and gives output to one variable
here i'm pasting my code below.
Updated
$sqlStr = 'call sp_testproc(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);';
$c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
$stmt = $c->prepare($sqlStr);
$bindValues = array(
'fields' => array(
array('type' => 'input', 'name' => ':From', 'value' => '[email protected]', 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':To', 'value' => '[email protected]', 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':Cc', 'value' => '', 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':Bcc', 'value' => '', 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':Subject', 'value' => 'some value given', 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':someId', 'value' => 10334, 'data_type' => PDO::PARAM_INT),
array('type' => 'input', 'name' => ':someId1', 'value' => 307560, 'data_type' => PDO::PARAM_INT),
array('type' => 'input', 'name' => ':ReadOrUnread', 'value' => 1, 'data_type' => PDO::PARAM_INT),
array('type' => 'input', 'name' => ':HasAttachments', 'value' => 1, 'data_type' => PDO::PARAM_INT),
array('type' => 'input', 'name' => ':someId2', 'value' => 9, 'data_type' => PDO::PARAM_INT),
array('type' => 'input', 'name' => ':someId3', 'value' => 89, 'data_type' => PDO::PARAM_INT),
array('type' => 'input', 'name' => ':link', 'value' => 'http:google.com', 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':someId4', 'value' => 3998, 'data_type' => PDO::PARAM_STR),
array('type' => 'input', 'name' => ':MailDate', 'value' => '2014-02-01', 'data_type' => PDO::PARAM_STR),
array('type' => 'output', 'value' => '$Id', 'data_type' => PDO::PARAM_INPUT_OUTPUT),
)
);
$proc_pass_val = null;
if (isset($bindValues['fields']) && is_array($bindValues['fields'])) {
$arg_cnt = 1;
for ($i = 0; $i < count($bindValues['fields']); $i++) {
$bindValue = $bindValues['fields'][$i]["value"];
$param_type = $bindValues['fields'][$i]["type"];
$data_type = $bindValues['fields'][$i]["data_type"];
if ($param_type == 'input') {
$stmt->bindValue($arg_cnt++, $bindValue, $data_type);
} elseif ($param_type == 'output') {
$stmt->bindParam($arg_cnt++, $proc_pass_val, $data_type);
}
}
}
$stmt->execute();
print "procedure returned $proc_pass_val\n";
Note: I'm using MSSQL server
I want the Procedure output in $Id variable, please suggest me where i went wrong
Thanks in Advance
I just passed the parameter length to bindParam like this
$stmt->bindParam($arg_cnt++, $proc_pass_val, $data_type, [Length of the data type])
and it worked for me
the code is more readable in this solution
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments