Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call stored procedure by using PHP and SQL Server 2008

I am using SQL Server 2008 with PHP. I want to call a stored procedure in PHP.

Please guide me.

Regards

like image 665
Deepak Avatar asked Feb 25 '23 16:02

Deepak


1 Answers

read mssql_execute()

$conn = mssql_connect($host, $user, $pass);
mssql_select_db('somedb', $conn);

// Call a simple query
$result = mssql_query('SELECT * FROM sometable', $conn);

// Release the result resource
mssql_free_result($result);

// Then execute the procedure
$proc = mssql_init('some_proc', $conn);
$proc_result = mssql_execute($proc);

// Etc...
mssql_free_statement($proc);

EDIT

read odbc_exec()

$conn = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
$exec = odbc_exec($conn, "CALL storedProc()");

and a very nice example from the php.net docs odbc_execute():

Examples

Example #1 odbc_execute() and odbc_prepare() example In the following code, $success will only be TRUE if all three parameters to myproc are IN parameters:

$a = 1;
$b = 2;
$c = 3;
$stmt    = odbc_prepare($conn, 'CALL myproc(?,?,?)');
$success = odbc_execute($stmt, array($a, $b, $c));

If you need to call a stored procedure using INOUT or OUT parameters, the recommended workaround is to use a native extension for your database (for example, mssql for MS SQL Server, or oci8 for Oracle).

like image 92
teemitzitrone Avatar answered Mar 17 '23 01:03

teemitzitrone