note 97531 added to pdostatement.execute

From: Date: Sat, 24 Apr 2010 17:05:18 +0000
Subject: note 97531 added to pdostatement.execute
Groups: php.notes 
Request: Send a blank email to php-notes+get-168591@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 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 ^^). ---- Server IP: 89.31.146.176 Probable Submitter: 87.91.64.42 ---- Manual Page -- http://www.php.net/manual/en/pdostatement.execute.php Edit -- https://master.php.net/note/edit/97531 Del: integrated -- https://master.php.net/note/delete/97531/integrated Del: useless -- https://master.php.net/note/delete/97531/useless Del: bad code -- https://master.php.net/note/delete/97531/bad+code Del: spam -- https://master.php.net/note/delete/97531/spam Del: non-english -- https://master.php.net/note/delete/97531/non-english Del: in docs -- https://master.php.net/note/delete/97531/in+docs Del: other reasons-- https://master.php.net/note/delete/97531 Reject -- https://master.php.net/note/reject/97531 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168591) next »