简体   繁体   中英

How to retrieve out parameter from mysqli stored procedure in php?

I want to access OUT parameter, from my mysqli database using stored procedures, and save it in php variable for printing message on web page.

My Stored Procedure Is:

DELIMITER $$
CREATE procedure insertUser (IN _name varchar(50), IN _username varchar(50), IN _password varchar(50), IN _email varchar(50), IN _cellNumber int(11), IN _estateName varchar(50), OUT _rply varchar(50))


BEGIN

DECLARE count1 int;
DECLARE count2 int;
DECLARE varEstateId int;
DECLARE _result varchar(50);

select COUNT(*) into count1 from users where username = _username;
select COUNT(*) into count2 from users where email = _email;

if (count1+count2 < 1)
THEN
insert into users (username , passkey , email) VALUES (_username ,_password,_email);
insert into estatedetails (estateName, estateOwnerUsername) 
values  (_estateName, _username);
SELECT estateId into varEstateId from estatedetails where estateOwnerUsername = _username; 
insert into usersdetail (username, name, cell1, estateId, accountType) 
VALUES (_username, _name, _cellNumber, varEstateId, 'owner');
set _result = 'inserted';
else 
set _result = 'alreadyExist';
end IF;
set _rply = _result;

END$$
DELIMITER;

My php side code for calling stored procedure is: (Incomplete Code)

$db = new mysqli('localhost', 'root', '', 'estatelink');
$stmt=$db->prepare("CALL insertUser(?,?,?,?,?,?,?)");
$stmt->bind_param('ssssiss',$name, $username, $password, $email, $num, $estate_name, $rply);
$stmt->execute();
$result= $rply;

Note: My query is executing perfectly inserting data with success. My only issue is that how can I access OUT parameter in this case it is $rply.

Thanks in Advance.

You do not use your output parameter. You can set its value, like this:

set @_rply = 'foobar';

You can use it, like this:

$db->multi_query( "CALL insertUser($name,$username,$password,$email,$cellNumber,$estateName,@_rply);SELECT @_rply as _rply" );
$db->next_result();            // flush the null RS from the call
$rs=$db->store_result();       // get the RS containing the id
echo $rs->fetch_object()->_rply, "\n";
$rs->free();

您可以在过程中使用SELECT语句,并像典型查询一样获取结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM