dsunj08246
2017-12-27 20:03
浏览 96
已采纳

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

Simple example

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

and In php

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

How can I get return value ( @resultnot or @resultOK ) in php? And How can I get and use return value in JSON? Like this

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

图片转代码服务由CSDN问答提供 功能建议

简单示例

  CREATE PROCEDURE [dbo]。[InsertLoginInPHP  ] 
 @login为nvarchar(50),@ password为nvarchar(50),@ resultOK为int,@ resultnot为int 
 
AS 
BEGIN 
 SET @resultOK = 10 
 SET @resultnot = 1 
  
如果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 
   
 
 

and in 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'=>' 好的','return'=> $ query_sotored [resultOK]))); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzou8074 2017-12-27 20:52
    已采纳

    You need to fetch a row from the statement, then access the column from the array this returns.

    $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'])))
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题