[PDO] Cannot execute queries while other unbuffered queries are active

[PDO] Cannot execute queries while other unbuffered queries are active

am 23.03.2008 23:25:29 von Fabian Golle

------=_NextPart_000_0110_01C88D3D.2FCEE9B0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hey,

I'm using a PDO-MySQL in a class of my Accounting-Framework. The problem: if
I call my function query(), see below, multiple times, I get the error:



Array ( [0] => HY000 [1] => 2014 [2] => Cannot execute queries while other
unbuffered queries are active. Consider using PDOStatement::fetchAll().
Alternatively, if your code is only ever going to run against mysql, you may
enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
attribute. ) 1



protected function query($query) {

try {

if (preg_match("@^select@is", $query)) {

$sth = $this->link->prepare($query) or
die(print_r($this->link->errorInfo()));

$sth->execute();



// Get RowCount

$rc = 0;

while($count = $sth->fetch(PDO::FETCH_ASSOC)) $rc++;

$this->count[(string)$sth] = $rc;

$sth->execute();

return $sth;

} else {

$this->link->query($query);

return true;

}

} catch (Exception $e) {

$dbh->rollBack();

echo "DATABASE OPERATION FAILED: " . $e->getMessage();

exit;

}

}



The Solution described in the official manual is:



$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);



My Connection function (which also includes the solution):



private function connect() {

$this->link = new
PDO('mysql:host=localhost;dbname=micronax_mymicronax', "mymicronax",
"l{7kD01eG-B[");

$this->link->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY , true);

return true;

}



$link is defined as: protected $link = false;





But I still get the damn error-message. What's going wrong here?



Other possibilities, ex. Adding the MYSQL_ATTR_USE_BUFFERED_QUERY to "new
PDO()" or adding it to "prepare($query)" doesn't show affect.



Please help me,



Greetings from Germany and happy easter,



--

Fabian Golle

Salisweg 22e | 63454 Hanau

Tel.: +49 6181 26644

Mobil: +49 172 8183994

Web: http://www.micronax.de

Email: f.golle@googlemail.com

(c) Micronax Systems 2007. All rights reserved




------=_NextPart_000_0110_01C88D3D.2FCEE9B0--

Re: [PDO] Cannot execute queries while other unbuffered queries areactive

am 24.03.2008 16:07:52 von Roberto Mansfield

Have you tried using closeCursor() after your query is finished?
-Roberto


Fabian Golle wrote:
> Hey,
>
> I'm using a PDO-MySQL in a class of my Accounting-Framework. The problem: if
> I call my function query(), see below, multiple times, I get the error:
>
>
>
> Array ( [0] => HY000 [1] => 2014 [2] => Cannot execute queries while other
> unbuffered queries are active. Consider using PDOStatement::fetchAll().
> Alternatively, if your code is only ever going to run against mysql, you may
> enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
> attribute. ) 1
>
>
>
> protected function query($query) {
>
> try {
>
> if (preg_match("@^select@is", $query)) {
>
> $sth = $this->link->prepare($query) or
> die(print_r($this->link->errorInfo()));
>
> $sth->execute();
>
>
>
> // Get RowCount
>
> $rc = 0;
>
> while($count = $sth->fetch(PDO::FETCH_ASSOC)) $rc++;
>
> $this->count[(string)$sth] = $rc;
>
> $sth->execute();
>
> return $sth;
>
> } else {
>
> $this->link->query($query);
>
> return true;
>
> }
>
> } catch (Exception $e) {
>
> $dbh->rollBack();
>
> echo "DATABASE OPERATION FAILED: " . $e->getMessage();
>
> exit;
>
> }
>
> }
>
>
>
> The Solution described in the official manual is:
>
>
>
> $db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
>
>
>
> My Connection function (which also includes the solution):
>
>
>
> private function connect() {
>
> $this->link = new
> PDO('mysql:host=localhost;dbname=micronax_mymicronax', "mymicronax",
> "l{7kD01eG-B[");
>
> $this->link->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY , true);
>
> return true;
>
> }
>
>
>
> $link is defined as: protected $link = false;
>
>
>
>
>
> But I still get the damn error-message. What's going wrong here?
>
>
>
> Other possibilities, ex. Adding the MYSQL_ATTR_USE_BUFFERED_QUERY to "new
> PDO()" or adding it to "prepare($query)" doesn't show affect.
>
>
>
> Please help me,
>
>
>
> Greetings from Germany and happy easter,
>
>
>
> --
>
> Fabian Golle
>
> Salisweg 22e | 63454 Hanau
>
> Tel.: +49 6181 26644
>
> Mobil: +49 172 8183994
>
> Web: http://www.micronax.de
>
> Email: f.golle@googlemail.com
>
> (c) Micronax Systems 2007. All rights reserved
>
>
>
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php