如何从php中的mysqli存储过程中检索出参数?

时间:2015-07-11 07:22:59

标签: php stored-procedures mysqli

我想使用存储过程从我的mysqli数据库访问OUT参数,并将其保存在php变量中,以便在网页上打印消息。

我的存储过程是:

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;

我调用存储过程的php端代码是:(不完整代码)

$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;

注意:我的查询正在成功执行完美插入数据。我唯一的问题是如何在这种情况下访问OUT参数$ rply。

先谢谢。

2 个答案:

答案 0 :(得分:0)

您不使用输出参数。您可以设置其值,如下所示:

set @_rply = 'foobar';

您可以使用它,如下所示:

$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();

答案 1 :(得分:0)

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