title: Accessing MySQL stored procedure output in Zend Framework 2
body: <p>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:</p>
  
  <pre><code>$result = $this-&gt;dbAdapter-&gt;query('CALL sp_register_user(?, ?)', array('username', 'password'));
  </code></pre>
  
  <p>I can also access any result sets returned from my stored procedure.</p>
  
  <p>What I want now is to have an output value from my stored procedure as a third parameter, so something like this:</p>
  
  <pre><code>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 //
  </code></pre>
  
  <p>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. <a href="http://php.net/manual/en/pdo.prepared-statements.php" rel="nofollow">Example 4 on this page</a> 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.</p>
  
  <p>Still, I tried to make it work with PDO directly, like this:</p>
  
  <pre><code>$username = 'my_username';
  $password = 'my_password';
  $code = 0;
  
  $stmt = $this-&gt;dbAdapter-&gt;createStatement();
  $stmt-&gt;prepare('CALL sp_register_user(?, ?, ?)');
  $stmt-&gt;getResource()-&gt;bindParam(1, $username);
  $stmt-&gt;getResource()-&gt;bindParam(2, $password);
  $stmt-&gt;getResource()-&gt;bindParam(3, $code, \PDO::PARAM_INT, 3);
  $stmt-&gt;execute();
  </code></pre>
  
  <p>However, I get an error saying that the statement could not be executed.</p>
  
  <p>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.</p>