如何获得MS SQL存储过程返回值在PHP和PDO?

问题描述 投票:1回答:1

简单的例子

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])));
php sql-server stored-procedures pdo
1个回答
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'])))
© www.soinside.com 2019 - 2024. All rights reserved.