note 97531 modified in pdostatement.execute by danbrown

From: Date: Sat, 24 Apr 2010 22:15:33 +0000
Subject: note 97531 modified in pdostatement.execute by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168605@lists.php.net to get a copy of this message
Note that you must - EITHER pass all values to bind in an array to PDOStatement::execute() - OR bind every value before with PDOStatement::bindValue(), then call PDOStatement::execute() with *no* parameter (not even "array()"!). Passing an array (empty or not) to execute() will "erase" and replace any previous bindings (and can lead to, e.g. with MySQL, "SQLSTATE[HY000]: General error: 2031" (CR_PARAMS_NOT_BOUND) if you passed an empty array). Thus the following function is incorrect in case the prepared statement has been "bound" before: <?php function customExecute(PDOStatement &$sth, $params = NULL) { return $sth->execute($params); } ?> and should therefore be replaced by something like: <?php function customExecute(PDOStatement &$sth, array $params = array()) { if (empty($params)) return $sth->execute(); return $sth->execute($params); } ?> Also note that PDOStatement::execute() doesn't require $input_parameters to be an array. (of course, do not use it as is ^^). --was-- Note that you must - EITHER pass all values to bind in an array to PDOStatement::execute() - OR bind every value before with PDOStatement::bindValue(), then call PDOStatement::execute() with *no* parameter (not even "array()"!). Passing an array (empty or not) to execute() will "erase" and replace any previous bindings (and can lead to, e.g. with MySQL, "SQLSTATE[HY000]: General error: 2031" (CR_PARAMS_NOT_BOUND) if you passed an empty array). Thus the following function is incorrect in case the prepared statement has been "bound" before: <?php function badCustomExecute(PDOStatement &$sth, array $params = array()) { return $sth->execute($params); } ?> and should therefore be replaced by something like: <?php function customExecute(PDOStatement &$sth, array $params = array()) { if (empty($params)) return $sth->execute(); return $sth->execute($params); } ?> (of course, do not use it as is ^^). http://php.net/manual/en/pdostatement.execute.php

« previous php.notes (#168605) next »