Home > Article > Backend Development > PHP calls MSSQL stored procedures based on ADODB_PHP tutorial
A few days ago I was working on PHP calling MSSQL stored procedures. It took a long time and there was very little online information (especially based on ADODB).
Here I will give an example of calling MSSQL stored procedures based on ADODB. I hope it will be helpful to others.
/*
Test out params - works in PHP 4.2.3 and 4.3.3 and 4.3.8 but not 4.3.0:
CREATE PROCEDURE at_date_interval
@days INTEGER,
@start VARCHAR(20) OUT,
@end VARCHAR(20) OUT
AS
BEGIN
set @start = CONVERT(VARCHAR(20), getdate(), 101)
set @end =CONVERT(VARCHAR(20), dateadd(day, @days, getdate()), 101 )
END
GO
*/
$db->debug=1;
$stmt = $db->PrepareSP('at_date_interval');
$days = 10;
$begin_date = '';
$end_date = '';
$db- >InParameter($stmt,$days,'days', 4, SQLINT4); //Here 97xxoo is the In parameter
$db->OutParameter($stmt,$begin_date,'start', 20, SQLVARCHAR) ; //This is the return value of the stored procedure, which can be obtained by directly accessing $begin_date.
$db->OutParameter($stmt,$end_date,'end', 20, SQLVARCHAR ); //Same as above
$db->Execute($stmt);
echo $begin_date;
echo $end_date;
That’s it. I believe everyone can understand this simple example, and the other principles are the same.
Also note here: $begin_date and $end_date must be assigned a null value first, otherwise the result will not get the value. This is what kept me engaged for so long.