note 95879 added to pdostatement.fetch

From: Date: Wed, 27 Jan 2010 01:52:33 +0000
Subject: note 95879 added to pdostatement.fetch
Groups: php.notes 
Request: Send a blank email to php-notes+get-165349@lists.php.net to get a copy of this message
Just thought I would point out something interesting that caught my eye. Once you retrieve a PDOStatement, whether for a prepared statement or a normal result set, the connection status of your PDO handle is no longer taken into consideration! The PHP Manual clearly states that to open a PDO connection, create a new Instance of the PDO Class. To close a PDO connection, destroy the Instance, possibly by setting it to a Null Reference. The PDO Instance in this case is your handle. Here is the interesting part. If you close the PDO connection, as described above, you can still continue to either execute a prepared statement or iterate through a result set! For example, the following code will work perfectly fine: <?php // Open Handle. $Handle = new PDO( 'mysql:host=localhost; dbname=LOG' , 'root' ); $Handle->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION ); // Prepare Statement. $Statement = $Handle->prepare( "SELECT * FROM ERROR" ); // Close Handle. $Handle = null; // Execute Statement. Amazing How There Is No Problem Even Though The Connection Is Closed! $Statement->execute(); // Iterate Result. Again, Amazing How There Is No Problem! while( $row = $Statement->fetch( PDO::FETCH_BOTH , PDO::FETCH_ORI_NEXT ) ) { } ?> You do not even need to prepare a statement. A native query also has no problems, like the following code: <?php // Open Handle. $Handle = new PDO( 'mysql:host=localhost; dbname=LOG' , 'root' ); $Handle->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION ); // Query Statement. $Statement = $Handle->query( "SELECT * FROM ERROR" ); // Close Handle. $Handle = null; // Iterate Result. $Statement->fetchAll( ); ?> I am not sure if this is a bug or not but its a big gottcha for developers coming from other envrionments, such as .NET or even those PHP developers used to using the native database functions before PDO came around. For example, using the native mysql_* Functions, the following will not work: <?php mysql_connect( "localhost" , "root"); mysql_select_db( "LOG" ); mysql_close( ); mysql_query( "SELECT * FROM ERROR" ); ?> Good Luck, ---- Server IP: 69.147.83.197 Probable Submitter: 217.54.224.164 ---- Manual Page -- http://www.php.net/manual/en/pdostatement.fetch.php Edit -- https://master.php.net/note/edit/95879 Del: integrated -- https://master.php.net/note/delete/95879/integrated Del: useless -- https://master.php.net/note/delete/95879/useless Del: bad code -- https://master.php.net/note/delete/95879/bad+code Del: spam -- https://master.php.net/note/delete/95879/spam Del: non-english -- https://master.php.net/note/delete/95879/non-english Del: in docs -- https://master.php.net/note/delete/95879/in+docs Del: other reasons-- https://master.php.net/note/delete/95879 Reject -- https://master.php.net/note/reject/95879 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165349) next »