I have a stored procedure on SQL Server and it works fine and returns the correct values accordingly.
The problem is when I want to get its return value on PHP after it does the insert
I saw similiar questions, but it was no close to this I think
SQL Code:
ALTER PROCEDURE [dbo].[AddBox] @boxName VARCHAR(30), @boxDesc VARCHAR(140), @error VARCHAR(50) OUTPUT AS BEGIN TRY IF (ISNULL(@boxName,'') = '') BEGIN SET @error = 'error 1' RETURN -100 END BEGIN TRANSACTION INSERT INTO TB_Boxes (boxName, boxDesc) VALUES (@boxName, @boxDesc) DECLARE @ID INT SET @ID = @@IDENTITY COMMIT TRANSACTION SET @error = 'OK' RETURN @ID END TRY BEGIN CATCH ROLLBACK TRANSACTION RETURN -1 END CATCH GO
PHP Code:
$sql = "EXEC ? = AddBox ?,?,?"; $return = 0; $boxName = $_POST['boxName']; $boxDesc = $_POST['boxDesc']; $error = ""; $param1 = array(&$return, SQLSRV_PARAM_OUT); $param2 = array($boxName, SQLSRV_PARAM_IN); $param3 = array($boxDesc, SQLSRV_PARAM_IN); $param4 = array(&$error, SQLSRV_PARAM_OUT); $params = array($param1, $param2, $param3, $param4); $stmt = sqlsrv_query($conn, $sql, $params); if( $stmt === false ) { echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true); exit; } echo "ID:" . $return . "<br>"; echo "error:" . $error;
If the boxName is empty, php shows $return ("-100") and $error ("error 1") but when the stored procedure does the insert I get the values that I set
PHP shows me:
ID: 0 error:
I don`t know why is not working
Notes: PHP version: 7.2.34
https://stackoverflow.com/questions/66084165/i-cant-get-the-return-id-of-an-insert-on-a-stored-procedure-from-php February 07, 2021 at 11:03AM
没有评论:
发表评论