我正在尝试将行插入2个表,然后在SQL Server 2012中的存储过程中进行选择,并且由于某种原因我在php(sqlsrv)中得到0行(在navicat中我确实得到1行)< / p>
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代码:
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;
}
如果它进入其他条件,我会得到0,可能是什么问题?
答案 0 :(得分:0)
嗨,我有同样的问题。我解决了 您只需在存储过程中放置“SET NOCOUNT ON”
例如
CREATE procedure mysampleprocedure
AS BEGIN TRANSACTION TRANS
set nocount on
insert into sampletable(id,data) values
(
101,
'abc'
);
SELECT count(*) AS id FROM sampletable AS id;
IF @@ERROR > 0
BEGIN
ROLLBACK TRANSACTION TRANS
end
ELSE
BEGIN
COMMIT TRANSACTION TRANS
end
GO