如何在PHP中使用PDO获取MS SQL存储过程返回值?

时间:2017-12-27 20:03:25

标签: php sql-server stored-procedures pdo

简单示例

CREATE PROCEDURE [dbo].[InsertLoginInPHP]
    @login as nvarchar(50), @password as nvarchar(50), @resultOK as int, @resultnot as int

AS
BEGIN
    SET @resultOK = 10
    SET @resultnot = 1

    IF EXISTS (SELECT * FROM Users WHERE Login = @login)
        BEGIN
            SELECT @resultnot as return // 1
        END
    ELSE
        BEGIN
            INSERT INTO Users (Login,Password) VALUES(@login,@password)
            SELECT @resultOK as return // 10
        END
END

和在php中

$query = ("EXECUTE InsertLogin :login, :password");
$query_stored = $conn->prepare($query);
$query_stored->bindvalue(':login', $login);
$query_stored->bindvalue(':password', $password);   
$query_stored->execute();

如何在php中获取返回值(@resultnot或@resultOK)? 如何在JSON中获取和使用返回值? 喜欢这个

json_encode(array('status'=>'Ok','return'=>$query_sotored[resultOK])));

1 个答案:

答案 0 :(得分:0)

您需要从语句中获取一行,然后从返回的数组中访问该列。

$query = ("EXECUTE InsertLogin :login, :password");
$query_stored = $conn->prepare($query);
$query_stored->bindvalue(':login', $login);
$query_stored->bindvalue(':password', $password);   
$query_stored->execute();
$row = $query_stored->fetch(PDO::FETCH_ASSOC);
echo json_encode(array('status'=>'Ok','return'=>$row['return'])))