note 126672 added to pdostatement.execute

From: Date: Thu, 09 Dec 2021 17:35:48 +0000
Subject: note 126672 added to pdostatement.execute
Groups: php.notes 
Request: Send a blank email to php-notes+get-217346@lists.php.net to get a copy of this message
It took me a long while to realize this and the documentation didn't seem very clear on using PDO_Statement::execute() on a SELECT statement type query, so I wanted to note this here. When preparing a SELECT query and then executing it using PDO_Statement::execute(), you can then simply proceed to use PDO_Statement::fetch() or PDO_Statement::fetchAll() on that same PDO_Statement object. This is no different than using PDO::query() to return a PDO_Statement object, and then calling PDO_Statement::fetch() on that object. This is because the PDO_Statement object is of course still a PDO_Statement object, and, as the PDO::query documentation (https://www.php.net/manual/en/pdo.query.php) says, PDO::query also "[p]repares and executes an SQL statement." <?php $pdo_statement = $my_pdo_object->prepare( "SELECT * FROM MyTable WHERE Field1 = 'this_string'" ); if ( true === $this->execute_safe_query( $pdo_statement ) ) { echo $pdo_statement->fetch(); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 73.75.105.69) ---- Manual Page -- https://php.net/manual/en/pdostatement.execute.php Edit -- https://main.php.net/note/edit/126672 Del: integrated -- https://main.php.net/note/delete/126672/integrated Del: useless -- https://main.php.net/note/delete/126672/useless Del: bad code -- https://main.php.net/note/delete/126672/bad+code Del: spam -- https://main.php.net/note/delete/126672/spam Del: non-english -- https://main.php.net/note/delete/126672/non-english Del: in docs -- https://main.php.net/note/delete/126672/in+docs Del: other reasons-- https://main.php.net/note/delete/126672 Reject -- https://main.php.net/note/reject/126672 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217346) next »