Last active
December 27, 2015 00:19
-
-
Save underhilllabs/7237400 to your computer and use it in GitHub Desktop.
Example CodeIgniter Controller that gets parameters back from a stored procedure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment