Re: PDO concerns

From: Date: Mon, 28 Feb 2005 15:45:01 +0000
Subject: Re: PDO concerns
References: 1 2 3 4 5 6 7 8  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-2094@lists.php.net to get a copy of this message
Thanks, while I suspect it would be rare for someone to mix execute and querys what you do see more often would be code like this $result = $db1->query('SELECT * FROM help_category'); while($row = $result->fetch()) { $result2 = $db1->query('SOME QUERY BUILT ON $row DATA'); } This is very common and will not work at all with PDO instead you have to $result =$db1->query('SELECT * FROM help_category'); $rows = $result->fetchall(); foreach($rows as $value) { $result = $db1->query('SOME QUERY BUILT ON $row DATA'); } Nothing wrong with that at all now that a fetchall exists it makes total sense to do it this way but being fetchall is "new" I can see a lot of people trying method 1 and getting frustrated with it not working. Richard Thomas - CEO Cyberlot Technologies Group Inc. 507.398.4124 - Voice Dan Scott wrote:
On Mon, 28 Feb 2005 09:27:07 -0600, Richard Thomas <lists@cyberlot.net> wrote:
I am sorry I feel like a idiot here ;) Because in my example I changed from $stm to $result the value of $stm was still open hence the cursor was still open The following does work. This should be put in the docs as clearly as possible to avoid confusion by anyone else.
Richard: I will update the docs to include a good warning about the use of rowCount() with SELECT statements, and to cover this particular gotcha. Thanks for the suggestion!
<> $stm = $db1->prepare('SELECT * FROM help_category LIMIT 1'); $stm->execute(); print_r($stm->fetch(PDO_FETCH_ASSOC)); $stm = $db1->prepare('SELECT * FROM help_category LIMIT 1'); $stm->execute(); print_r($stm->fetch(PDO_FETCH_ASSOC)); $stm = $db1->query('SELECT * FROM help_category LIMIT 1'); print_r($stm->fetch(PDO_FETCH_ASSOC)); $stm = $db1->query('SELECT * FROM help_category LIMIT 1'); print_r($stm->fetch(PDO_FETCH_ASSOC)); Thank you, sorry it took me so long to understand what was going on. Richard Thomas - CEO Cyberlot Technologies Group Inc. 507.398.4124 - Voice Wez Furlong wrote:
It works because you are re-assigning $stm with a new value; that causes the old $stm to be deleted prior to creating the new one; in other words, it doesn't have to be NULL that you assign to close/delete the statement object. --Wez. On Mon, 28 Feb 2005 08:23:00 -0600, Richard Thomas <lists@cyberlot.net> wrote:
     
There is something diffrent however between a PDO::query and a PDO::prepare and execute, Because 2 prepare/executes in a row work without having to declare anything NULL, Run the code there is no confusion on my part in how this works, Well there was confusion in that I assumed that since 2 prepare/executes do work without declaring the stm null the fact that 2 querys didn't had to be a bug in my mind. $dsn = 'mysql:dbname=mysql;host=localhost'; $db1 = new PDO($dsn, 'root', ''); $db1->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION); $stm = $db1->prepare('SELECT * FROM help_category LIMIT 1'); $stm->execute(); print_r($stm->fetch(PDO_FETCH_ASSOC)); $stm = $db1->prepare('SELECT * FROM help_category LIMIT 1'); $stm->execute(); print_r($stm->fetch(PDO_FETCH_ASSOC)); Shouldn't work but does?
       
     
-- PECL development discussion Mailing List (http://pecl.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.pecl.dev (#2094) next »