title: Calling MYSQL stored functions (not stored procedures) in Zend Framework
body: <p>Is there a way to call a MySQL stored function (not procedure) in Zend Framework? Or work around the framework to use unfiltered PHP code?</p>
  
  <p>In a 'pure' php application, the following code works - using the mysqli db connection (not mysql). However in Zend framework, this code will not cause an application error but will also not bind the result to the php variable.</p>
  
  <p>If you use a Zend_Db adapter, there is no way I can find to bind the result of the function to a variable (and thus get the answer - there is no Zend_Db method equivalent to 'bind_result').</p>
  
  <p>I have looked at previous questions but they focus on stored procedures (e.g. <a href="https://stackoverflow.com/questions/1303912/how-can-i-use-a-stored-procedure-in-a-mysql-database-with-zend-framework">How can I use a stored procedure in a MySql database with Zend Framework?</a> ).</p>
  
  <p>Thanks.</p>
  
  <p>UPDATE: Since writing, I've discovered that this works fine with MYSQL on MAMP, but not with MYSQL on Zend Server CE on Mac. </p>
  
  <p><strong>MySQL stored function:</strong></p>
  
  <pre><code>CREATE FUNCTION GetUserAccountType(
  inUsername  VARCHAR(50)
  ) RETURNS INT(3)
  BEGIN
      SELECT AccountType_i INTO @retVal FROM UserTable WHERE Username_vc = inUsername;
      RETURN @retVal;
  END;
  GO
  </code></pre>
  
  <p><strong>Php code calling the MySQL function:</strong></p>
  
  <pre><code>// open mysqli connection with db
  $my_db = new mysqli($db_hostname, $db_username, $db_password, $db_database);
  
  //  Prepare MySQL function ($userName is a string)
  
  $stmt=$my_db-&gt;prepare("SELECT GetUserAccountType(?)") or die($my_db-&gt;error); 
  $stmt-&gt;bind_param('s', $userName) or die($stmt-&gt;error);
  $stmt-&gt;execute() or die($stmt-&gt;error); 
  $stmt-&gt;bind_result($result);   
  $stmt-&gt;fetch();
  
  // (in php the $result variable  is now set as the result of the MySQL function, @retVal.
  // In Zend framework, the $result variable is set to FALSE)
  return $result;
  </code></pre>