简体   繁体   中英

Executing SQL Stored Procedures with Input and Output parameters

I am very new to stored procedures and the PDO library so please take it easy on me. I am trying to execute a stored procedure using a PDO function and retrieve the recordsets along with the output parameters. Which function should I use and what parameters are necessary?

If I want to execute basic statements using the function like the following example, what PDO method should I use? eg

$mssql = new mssql;

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

I want the function to return the correct results dependant on the statement type. Is this going to be possible?

UPDATE

Just in case I didnt make it very clear, the 'mssql' class uses the PDO class to execute queries. At the moment I am using:

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

After researching around the internet, I found it is in fact very simple using the PDO prepare, bindParam and execute methods.

An example of a store procedure using my class:

$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)}");

This is converted to:

$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();

The recordsets can then be retrieved using:

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());

And the input and output parameters can be retrieved using the array:

$returned_parameters

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM