note 115510 added to pdostatement.bindvalue

From: Date: Thu, 07 Aug 2014 09:47:49 +0000
Subject: note 115510 added to pdostatement.bindvalue
Groups: php.notes 
Request: Send a blank email to php-notes+get-200133@lists.php.net to get a copy of this message
The reason that we cannot define the value variable for bindValue() after calling it, is because that it binds the value to the prepared statement immediately and does not wait until the execute() to happen. The following code will issue a notice and prevent the query from taking place: <?php $st = $db->prepare ("SELECT * FROM posts WHERE id= :val "); $st->bindValue(':val',$val); $val = '2'; $st->execute(); ?> The output: Notice: Undefined variable: val. Whereas in the case of bindParam, the evaluation of the value to the parameter will not be performed until the call of execute(). And that's to gain the benefit of reference passing. <?php $st = $db->prepare ("SELECT * FROM posts WHERE id = :val "); $st->bindParam(':val',$val); $val = '2'; // // some code // $val = '3'; // re-assigning the value variable $st->execute(); ?> works fine. ---- Server IP: 72.52.91.14 Probable Submitter: 2.89.203.60 ---- Manual Page -- http://php.net/manual/en/pdostatement.bindvalue.php Edit -- https://master.php.net/note/edit/115510 Del: integrated -- https://master.php.net/note/delete/115510/integrated Del: useless -- https://master.php.net/note/delete/115510/useless Del: bad code -- https://master.php.net/note/delete/115510/bad+code Del: spam -- https://master.php.net/note/delete/115510/spam Del: non-english -- https://master.php.net/note/delete/115510/non-english Del: in docs -- https://master.php.net/note/delete/115510/in+docs Del: other reasons-- https://master.php.net/note/delete/115510 Reject -- https://master.php.net/note/reject/115510 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#200133) next »