I have a stored procedure in SQL Server 2012 which first checks if data exists, if it does then it sets the Output parameter @Return to 1 and runs a select query and if not, sets @Return to 0 and returns a different select query.
When testing this stored procedure to ensure the data is accurate it is perfect and returns the data I am expecting. The problem lies on the PHP side when trying to read the output parameter it is showing ��t_rrr when it should be showing a 1 or 0. I believe the problem may be in the Predefined Constant in the sqlsrv_query but i cannot seem to get it working. Here is my code:
PHP:
if(isset($_GET['accno'])) {
$search = $_GET['accno'];
$return = "";
$tsql_callSP = "EXEC Intranet.CustomerSearch @Search=?, @Return=?";
$params = array(
array($search, SQLSRV_PARAM_IN),
array($return, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR),SQLSRV_SQLTYPE_INT),
);
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql_callSP, $params);
if( $stmt === false )
{
echo "EXEC Intranet.CustomerSearch Failed\n";
die( print_r( sqlsrv_errors(), true));
}
while($res = sqlsrv_next_result($stmt))
{
// make sure all result sets are stepped through,
// since the output params may not be set until this happens
}
echo $return;
if($return == 0) { ?>
//1st Result Set
while($row = sqlsrv_fetch_array($stmt)) {
}
<?php } elseif($return == 1) { // End if Return 0 ?>
//2nd Result Set
while($row = sqlsrv_fetch_array($stmt)) {
}
<?php } // End if Return 1 ?>
SQL Stored Procedure:
USE [Intranet]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [Intranet].[CustomerSearch]
@Search nvarchar(10)
,@Return int output
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS
(
SELECT KeyCode
FROM Autopart.dbo.Customer
WHERE KeyCode = @Search
)
BEGIN
SELECT
Customer.KeyCode
,Customer.X13
,Customer.Name
,Customer.Addra
,Customer.Addrb
,Customer.Addrc
,Customer.Addrd
,Customer.Addre
,Customer.PCode
,Customer.CTitle
,Customer.Climit
,Customer.Ptype
,Customer.Stel
,Customer.SCont
,Customer.ACont
,Customer.XString5
,Customer.Locked
,Customer.Email
,Customer.StopStatus
,CusNotes.Comment
FROM
Autopart.dbo.Customer
LEFT OUTER JOIN Autopart.dbo.CusNotes ON Autopart.dbo.Customer.KeyCode = Autopart.dbo.CusNotes.Account
WHERE
(Customer.KeyCode = @Search)
AND (CusNotes.Seqno = 1 OR CusNotes.Seqno IS NULL)
SET @Return = 1
END ELSE BEGIN
SELECT TOP 100
KeyCode
,Name
,PCode
,Addra
,Addrb
,Addrc
FROM
AUTOPART.dbo.Customer
WHERE
(KeyCode LIKE '%'+@Search+'%'
OR Name LIKE '%'+@Search+'%'
OR PCode LIKE '%'+@Search+'%')
SET @Return = 0
END
END
I have tried changing the PHP and SQL types around but can't get the desired result. What is strange is if I create a stored procedure that is an INSERT or UPDATE statement the OUTPUT returns correctly.
EDIT:
SQL Server Stored Procedure Collation is Latin1_General_CI_AS
$return = 0;instead of$return = "";andSQLSRV_PARAM_INOUTinstead ofSQLSRV_PARAM_OUTandSQLSRV_PHPTYPE_INT(SQLSRV_ENC_BINARY)instead ofSQLSRV_PHPTYPE_STRING.$returnvariable as suggested, changed the param type toSQLRV_PARAM_INOUTand the PHPTYPE toarray($return, SQLSRV_PARAM_INOUT, SQLSRV_PHPTYPE_INT,SQLSRV_SQLTYPE_INT),Now i am always returning a 0 regardless of query (No silly characters though) andmb_detect_encodingdetects as ASCII