在Laravel 10中,我使用原始的PHP代码来调用一个,因为我的Sp返回2个表。但我无法以任何方式接收输出参数。有没有办法得到输出参数?
代码:
public function login(Request $request)
{
$outputParam = "null";
$outputParam2 = "null";
$conn = DB::connection('sqlsrv');
$sql = Db::select('SET NOCOUNT ON; EXEC sp_Login ' . $outputParam . ',' . $outputParam2.',Login,GetAll_for_login,null,admin,55e01f2a13f587e1e9e4aedb8242d,null, null, null, null, null, null, null, null, null, null, null, null, null, null;'); //, array($request->in1, $request->in2));
// I'm sending login details as hard coded for testing.
$pdo = $conn->getPdo()->prepare($sql);
$pdo->execute();
$rows = array();
$res = array();
// Iterate through the recordsets
do {
$rows = $pdo->fetchAll();
array_push($res, $rows);
} while ($pdo->nextRowset());
$data = $res[0];
$data2 = $res[1];
return $this->sendResponse('Done', ['maindata' => $res, "param1" => $outputParam, "param2" => $outputParam2]);
//return $this->sendResponse('Done', ['data' => $data, 'data2' => $data2]);
}
下面是我在API中调用函数后在Postman中接收到的数据:
{
"success": true,
"data": "Done",
"message": {
"data": [
[
{
// Data from First Table
}
],
[
{
// Data from Second Table
},
]
],
"param1": "null",
"param2": "null"
}
}
我以前尝试过这样传递,但它显示语法错误:
$sql = "SET NOCOUNT ON ; EXEC sp_Login
@pSuccessFlag = " . $outputParam . " ,
@pMessage = " . $outputParam2 . " ,
@pCallingPage = Login,
@pOperation = GetAll_for_login,
@pid = " . null . ",
@puname = admin,
@ppassword = c12e01f2a13ff5587e1e9e4aedb8242d,
@premark = " . null . ",
@POpwd = " . null . ",
@PROLE_ID = " . null . ",
@PIS_ACTIVE = " . null . ",
@POTP = " . null . ",
@POTP_DATE " . null . ",
@PFACILITY_ID = " . null . ",
@PCREATED_BY = " . null . ",
@PCREATED_ON = " . null . ",
@PMODIFIED_BY = " . null . ",
@PMODIFIED_ON = " . null . ",
@PUSER_TYPE_ID = " . null . ",
@PEMP_ID = " . null . ",
@PSERVER_LOGTIME = " . null . ";";
$pdo = $conn->getPdo()->prepare($sql);
$pdo->execute();
以下是SP中我的参数的数据类型:
CREATE PROCEDURE "sp_Login"(
IN "@pSuccessFlag" VARCHAR(2) OUTPUT,
IN "@pMessage" VARCHAR(2000) OUTPUT,
IN "@pCallingPage" VARCHAR(255),
IN "@pOperation" VARCHAR(255),
IN "@pid" int,
IN "@puname" varchar(255),
IN "@ppassword" varchar(50),
IN "@premark" varchar(50),
IN "@POpwd" varchar(50),
IN "@PROLE_ID" int,
IN "@PIS_ACTIVE" varchar(15),
IN "@POTP" varchar(50),
IN "@POTP_DATE" DATETIME,
IN "@PFACILITY_ID" INT,
IN "@PCREATED_BY" varchar(50),
IN "@PCREATED_ON" DATETIME,
IN "@PMODIFIED_BY" varchar(50),
IN "@PMODIFIED_ON" DATETIME,
IN "@PUSER_TYPE_ID" INT,
IN "@PEMP_ID" INT,
IN "@PSERVER_LOGTIME" DATETIME
)
1条答案
按热度按时间m1m5dgzv1#
您需要绑定输出参数(使用
PDOStatement::bindParam()
),而不是在SQL语句中注入它们的值。包括每个参数的数据类型和方向(使用PDO::PARAM_*
常量),如果是输出参数,则包括参数的长度。