2012-03-16 236 views
0

我對存儲過程和PDO庫非常陌生,所以請對我輕鬆一點。我正在嘗試使用PDO函數執行存儲過程,並隨輸出參數一起檢索記錄集。我應該使用哪個函數以及哪些參數是必需的?使用輸入和輸出參數執行SQL存儲過程

如果我想使用如下例子的函數執行基本語句,我應該使用什麼PDO方法?例如

$mssql = new mssql; 

$mssql->sql('stored_procedure()'); 
$mssql->sql('SELECT * FROM [test]'); 

我希望函數返回正確的結果取決於語句類型。這是可能的嗎?

UPDATE

萬一我沒有使它非常清楚,「MSSQL」類使用PDO類來執行查詢。目前我正在使用:

$PDO->prepare(); 
$PDO->exec(); 

回答

0

經過圍繞互聯網的研究,我發現它實際上很簡單,使用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 

我希望這有助於

相關問題