如何在php中使用pdo向存储过程传递多个参数

如何在php中使用pdo向存储过程传递多个参数,php,sql-server,stored-procedures,pdo,Php,Sql Server,Stored Procedures,Pdo,我有一个程序,它把多个参数作为输入,把输出给一个变量 在这里,我将我的代码粘贴到下面 已更新 $sqlStr = 'call sp_testproc(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);'; $c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password"); $stmt = $c->prepare($sqlStr); $bindValues = array( '

我有一个程序,它把多个参数作为输入,把输出给一个变量

在这里,我将我的代码粘贴到下面

已更新

$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' => 'adsfad@adsf.com', 'data_type' => PDO::PARAM_STR),
        array('type' => 'input', 'name' => ':To', 'value' => 'anil.g@adf.com', '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";
$sqlStr='call sp_testproc(?,,,,,,,,,,,,,,,,,,,,,,;';
$c=新PDO(“sqlsrv:Server=localhost;Database=testdb”、“用户名”、“密码”);
$stmt=$c->prepare($sqlStr);
$bindValues=数组(
“字段”=>数组(
数组('type'=>'input','name'=>':From','value'=>'adsfad@adsf.com“,”数据类型“=>PDO::PARAM_STR),
数组('type'=>'input','name'=>':To','value'=>'anil。g@adf.com“,”数据类型“=>PDO::PARAM_STR),
数组('type'=>'input','name'=>':Cc','value'=>'','data\u type'=>PDO::PARAM\u STR),
数组('type'=>'input','name'=>':Bcc','value'=>'','data_type'=>PDO::PARAM_STR),
数组('type'=>'input','name'=>':Subject','value'=>'给定的某些值','data\u type'=>PDO::PARAM\u STR),
数组('type'=>'input','name'=>':someId','value'=>10334,'data\u type'=>PDO::PARAM\u INT),
数组('type'=>'input','name'=>':someId1','value'=>307560,'data\u type'=>PDO::PARAM\u INT),
数组('type'=>'input','name'=>':ReadOrUnread','value'=>1,'data\u type'=>PDO::PARAM\u INT),
数组('type'=>'input','name'=>':HasAttachments','value'=>1,'data\u type'=>PDO::PARAM\u INT),
数组('type'=>'input','name'=>':someId2','value'=>9','data\u type'=>PDO::PARAM\u INT),
数组('type'=>'input','name'=>':someId3','value'=>89',data\u type'=>PDO::PARAM\u INT),
数组('type'=>'input','name'=>':link','value'=>'http:google.com','data\u type'=>PDO::PARAM\u STR),
数组('type'=>'input','name'=>':someId4','value'=>3998',data\u type'=>PDO::PARAM\u STR),
数组('type'=>'input','name'=>':MailDate','value'=>'2014-02-01','data_type'=>PDO::PARAM_STR),
数组('type'=>'output','value'=>'$Id','data_type'=>PDO::PARAM_INPUT_output),
)
);
$proc_pass_val=null;
if(isset($bindValues['fields'])和&is_数组($bindValues['fields'])){
$arg_cnt=1;
对于($i=0;$ibindValue($arg\u cnt++,$bindValue,$data\u type);
}elseif($param_type=='output'){
$stmt->bindParam($arg\u cnt++,$proc\u pass\u val,$data\u type);
}
}
}
$stmt->execute();
打印“过程返回$proc\u pass\u val\n”;
注意:我正在使用MSSQL服务器

我希望程序输出为$Id变量,请告诉我哪里出错了


提前感谢

我刚刚像这样将参数长度传递给了bindParam

$stmt->bindParam($arg_cnt++, $proc_pass_val, $data_type, [Length of the data type])
这对我很有效


代码在此

中更具可读性。。。使用问号占位符,这将是参数的1索引位置…
$arg\u cnt
未初始化,因此将
$arg\u cnt=1
放在循环之前,或者使用
+$arg\u cnt
bindParam第二个arg应该是
$$bindValue
我想让变量位于
$Id
中,所以我这样传递,如果我传递string literal,我想它会引发一个错误,无法通过引用传递param 2为什么要使用数组和循环?为什么你不能一个接一个地编写你的绑定?@YourCommonSense这是一个单一的调用而不是多个插入循环用于参数绑定过于复杂