Accessing MySQL stored procedure output in Zend Fr

2019-02-10 16:05发布

I have a simple MySQL stored procedure that takes two parameters and inserts a row into a table. I can execute it just fine from Zend Framework 2 like this:

$result = $this->dbAdapter->query('CALL sp_register_user(?, ?)', array('username', 'password'));

I can also access any result sets returned from my stored procedure.

What I want now is to have an output value from my stored procedure as a third parameter, so something like this:

DELIMITER //
CREATE PROCEDURE sp_register_user(IN username VARCHAR(50), IN password VARCHAR(128), OUT code INTEGER)
NOT DETERMINISTIC
COMMENT 'Registers a user'
  BEGIN
    INSERT INTO user VALUES (username, password);
    SET code = 123;
  END //

The question is how I can access this output variable from PHP (ZF2). I have only been able to find examples of how to do it directly through PDO, which I am using. Example 4 on this page shows how to do it through PDO directly. My concern is that if I use the PDO object directly, I am losing some abstractions and I am thereby assuming that I will always be using PDO.

Still, I tried to make it work with PDO directly, like this:

$username = 'my_username';
$password = 'my_password';
$code = 0;

$stmt = $this->dbAdapter->createStatement();
$stmt->prepare('CALL sp_register_user(?, ?, ?)');
$stmt->getResource()->bindParam(1, $username);
$stmt->getResource()->bindParam(2, $password);
$stmt->getResource()->bindParam(3, $code, \PDO::PARAM_INT, 3);
$stmt->execute();

However, I get an error saying that the statement could not be executed.

The ideal solution would be one where I could make use of ZF2's abstraction layer, but any ideas on how to access the output parameter are welcome and appreciated.

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-10 16:55

this must work, because i m using it :

    $str = "DECLARE @Msgvar   varchar(100);DECLARE @last_id int;

    exec CallEntry_Ins $CallLoginId,".$this->usrId .",@Msg = @Msgvar OUTPUT,@LAST_ID = @last_id OUTPUT;

    SELECT  @Msgvar AS N'@Msg',@last_id AS '@LAST_ID'; ";


    $stmt = $db->prepare($str);
    $stmt->execute();
    $rtStatus = $stmt->fetchAll();



     $rtStatus[0]["@LAST_ID"] //accessing Op para
查看更多
登录 后发表回答