I am trying to call a MSSQL stored procedure from PHP using ODBC and I guess it is fairly working. I mean, the procedure gets invoked. I get no error. But I am having some trouble retrieving output params.
The procedure does not return anything, but it has one output parameter, which is a varchar
and I need to get this data.
Here is the code in SQL:
CREATE PROCEDURE [dbo].[ProcName]
@peCodFact varchar(max) = NULL,
@peCodsAlmcs varchar(max) = NULL,
@peFechaConfDesde date = NULL,
@peFechaConfHasta date = NULL,
@peCodMed varchar(50) = NULL,
@psRegEjec varchar(max) = '<>' OUTPUT
AS
BEGIN TRY
/* data processing where @psRegEjec is set */
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
All the input params can be null, and the param I need to retrieve is @psRegEjec
.
Here's the code in PHP:
$idCon = odbc_connect($dns, $user, $passwd);
if (empty($idCon))
throw new Exception(odbc_errormsg());
$query = 'DECLARE @psRegEjec varchar(max); ';
$query .= 'EXEC BDName.dbo.ProcName @psRegEjec = @psRegEjec OUTPUT; ';
$query .= 'SELECT @psRegEjec AS data; ';
$res = odbc_exec($idCon, $query);
$datosRes = @odbc_fetch_array($res);
I've tried to retrieve the results with odbc_fetch_object
, ``odbc_fetch_array`, and all get the same. The results I'm getting is something like ...
\x0ø\x14\x7cen\x0)\x0\x0\x0\x11\x0\x0\x0Hø\x11\x7ado_nm_grupo_gestion\x0\xEÔ\x0\x18\x12\x7)\x0\x0\x0)\x0\x0\x0˜ø\x11\x7ccion_co_descripcion\x0\xF\x12\x7è\x11\x12\x7)\x0\x0\x0)\x0\x0\x0 ø\x11\x7ado_nm_funcionalidad\x0\xEÔ\x0P\x16\x12\x7)\x0\x0\x0)\x0\x0\x0 ù\x11\x7orporacion_descripcion\x0\x7 \x1B\x12\x7)\x0\x0\x0)\x0\x0\x0pø\x11\x7raseña anterior\x0\x0ˆ\x1F\x12\x7\x19\x0\x0\x0)\x0\x0\x0hú\x11\x7ripcion\x0\x0\x0\x0\x0y\x0\x0\x0\x19\x0\x0\x0\xF\x1.
and it goes on, pretty large... So I really don't know what to do. I saw in some article, that is required to create a second procedure, to return the output param from the first one, as ODBC driver does not suppor procedures output params ...
Is that really so? Is there any other way?