I am trying to insert row into 2 tables and then do select in a stored procdure in SQL Server 2012, and for some reason I get 0 rows in php (sqlsrv) (in navicat I do get 1 row as should)
ALTER PROCEDURE [dbo].[Register]
@FacebookId AS INT,
@SessionKey AS VARCHAR(255),
@Name AS NVARCHAR(255),
@Gender AS INT = 0,
@ProfilePicture AS VARCHAR(255)
AS
BEGIN
IF NOT EXISTS(SELECT Id FROM Users WHERE FacebookId = @FacebookId)
BEGIN
INSERT INTO Users(FacebookId,SessionKey) VALUES (@FacebookId,@SessionKey)
DECLARE @Id AS INT
SET @Id = scope_identity()
INSERT INTO UserData(Id,Name,Gender,ProfilePicture) VALUES (scope_identity(),@Name,@Gender,@ProfilePicture)
SELECT 1 AS success
END
ELSE
BEGIN
SELECT 0 AS success
END
END
php code:
function Register($facebookId,$sessionKey,$name,$gender,$profilePicture)
{
return SingleResponseRequest('Register',array('@FacebookId','@SessionKey','@Name','@Gender','@ProfilePicture'),
array($facebookId,$sessionKey,$name,$gender,$profilePicture),'success');
}
function SingleResponseRequest($procedureName,$procedureValues,$valuesArray,$returnColumn)
{
require_once __DIR__ . '/Configuration/Connection.php';
$db = new Connection();
$values = '';
foreach($procedureValues as $value)
$values .= $value . ' = ? ,';
$values = substr($values,0,strlen($values)-1);
$query = "EXEC $procedureName $values";
$res = $db->Query($query,$valuesArray);
$row_count = sqlsrv_num_rows( $res );
echo 'rows: '. $row_count;
if ($row_count === false)
echo "Error in retrieving row count.";
$row = sqlsrv_fetch_array( $res, SQLSRV_FETCH_ASSOC);
return $row[$returnColumn];
}
public function Query($query,$params)
{
for($i = 0 ; $i < count($params); $i++)
{
$params[$i] = $this->ms_escape_string($params[$i]);
$params[$i] = utf8_encode($params[$i]);
}
$options = array( "Scrollable" => SQLSRV_CURSOR_CLIENT_BUFFERED );
$result = sqlsrv_query($this->connection, $query, $params,$options);
if ( $result === false )
{
echo "Error in statement execution.<br>";
echo $query;
die( print_r( sqlsrv_errors(), true));
}
return $result;
}
I do get 0 if it goes to the else condition, what could be the problem?