ZF-1440: Binding parameters and results to PHP variables is broken


The function bindParam(), bindValue(), and bindColumn() need some bugfixing. These are not working with any Zend_Db adapters currently.


From my testing this seems to be an issue with PDO and the way the execute wrapper is defined:

<? /** * Executes a prepared statement. * * @param array $params OPTIONAL Values to bind to parameter placeholders. * @return bool * @throws Zend_Db_Statement_Exception */ public function execute(array $params = array()) { try { return $this->_stmt->execute($params); } catch (PDOException $e) { require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception($e->getMessage()); } } ?>

if you execute the object without any parameters to the Zend execute method:


$params is assigned an empty array. PDO them seems to use this empty array rather than the parameters from bindParam etc. if you change the function declaration to:

public function execute(array $params = null)

everything begins to work again as without an input it now passes NULL to the PDO execute method rather than an empty array.

Also, the default NULL values for $type in bindValue() (and possibly others) is also causing issues:

public function bindValue($parameter, $value, $type = null)

If you bind a value without specifying the $type with a PDO::PARAM_* value then the value becomes null when executed.

I suggest changing it to:

public function bindValue($parameter, $value, $type = null) { if (is_string($parameter) && $parameter[0] != ':') { $parameter = ":$parameter"; } try { if(is_null($type)) { return $this->_stmt->bindValue($parameter, $value); } return $this->_stmt->bindValue($parameter, $value, $type); } catch (PDOException $e) { require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception($e->getMessage()); } }

Thanks for the help debugging, Jeffrey! That did it -- at least for PDO adapters.

The fixes you suggest have been committed in revision 5048.

I'm going to leave this issue open for now, because I'd like to get it working across all adapters.

This fix was included in 1.0.0 RC2, at least for PDO adapters.

I have opened ZF-1475, ZF-1476, ZF-1477 for these binding features in IBM DB2, Oracle, and Mysqli.

Link to ZF-1475.

Link to ZF-1476.

Link to ZF-1477.