使用输入和输出参数执行SQL存储过程
问题描述:
我对存储过程和PDO库非常陌生,所以请对我轻松一点。我正在尝试使用PDO函数执行存储过程,并随输出参数一起检索记录集。我应该使用哪个函数以及哪些参数是必需的?使用输入和输出参数执行SQL存储过程
如果我想使用如下例子的函数执行基本语句,我应该使用什么PDO方法?例如
$mssql = new mssql;
$mssql->sql('stored_procedure()');
$mssql->sql('SELECT * FROM [test]');
我希望函数返回正确的结果取决于语句类型。这是可能的吗?
UPDATE
万一我没有使它非常清楚,“MSSQL”类使用PDO类来执行查询。目前我正在使用:
$PDO->prepare();
$PDO->exec();
答
经过围绕互联网的研究,我发现它实际上很简单,使用PDO prepare,bindParam和execute方法。使用我类
一存储过程的一个例子:
$mssql->bind_params = array(
'BatchNum' => array(
'value' => 'SS000008'
),
'RecCount' => array(
'value' => 0,
'type' => PDO::PARAM_INT,
'length' => 8
)
);
$mssql->sql("{CALL sp_Batch_Sales(@paramOne=:paramOne,@paramTwo=:paramTwo)}");
此被转换成:使用
$query = $PDO->prepare({CALL sp_Batch_Sales(@paramOne=:paramOne,@paramTwo=:paramTwo)});
$query->bindParam(':paramOne',$returned_parameters['paramOne']);
$query->bindParam(':paramTwo',$returned_parameters['paramTwo'],PDO::PARAM_INT,8);
$query->execute();
的记录集然后可以检索到:
do{
// Get the results
$results = $query->fetchAll(PDO::FETCH_ASSOC);
// Only place the results into the array if they exist
if(!empty($results)){
$data[$i]['results'] = $results;
}
// Set the number of rows in the result array
$data[$i]['rows'] = $query->rowCount();
// Increment i
$i++;
}while($query->nextRowset());
而输入和输出参数可以使用阵列检索:
$returned_parameters
我希望这有助于