简单的例子
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])));
你需要从语句读取行,然后从这个返回数组访问列。
$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'])))