본문 바로가기

Development/PHP

SQL Server Driver - Case 1 <미테스트>

class Example_controller extends CI_Controller {
 
// …
// Others attributes and methods omitted for brevity
 
/*
* Unfortunatelly, CodeIgniter does not work well with Stored Procedure with returning parameters,
* so, if you want to use any kind of Stored Procedure with parameters, it's better do it mannualy.
*/
public function some_function($parameter1, $parameter2)
{
// Connect to DB. You can't pass $this->db, cause it's an object and the connection info
// needs and Connection resource.
$serverName = "10.5.6.11";
$connectionInfo = array( "Database"=>"databasename", "UID"=>"username", "PWD"=>"secretword");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
 
/* Define the Transact-SQL query. Use question marks in place of
the parameters to be passed to the stored procedure */
$tsql_callSP = "{call zp_contas_receber_inc_congresso(?, ?, ?)}";
 
/*
* Define the parameter array. Put all parameter in the order they appear in the SP.
* The second argument is needed to say if the parameter is an INPUT or an OUTPUT
*/
 
// This must be set as Integer, cause the initial type for the OUTPUT
$output_parameter = 0;
 
$params = array(
array($parameter1, SQLSRV_PARAM_IN),
array($parameter2, SQLSRV_PARAM_IN),
array($output_parameter, SQLSRV_PARAM_OUT)
);
 
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt === false )
{
echo "Error in executing statement.\n";
die( print_r( sqlsrv_errors(), true));
}
 
/*Free the statement and connection resources. */
sqlsrv_free_stmt($stmt);
return $output_parameter;
}
}