Uncaught exception error PDOException with message 'SQLSTATE [HY000]: General error: 2014 [closed]

1

I have a code that works perfectly on the local server wampserver, it is a query interspersing the results the problem is that when I finish the site and host it I get an error.

The code is this:

$conn->exec('SET @orderA := 0; SET @orderB := 0;');
$q = $conn->prepare("
SELECT id, voz
FROM (
    SELECT id, voz
    ,IF(voz = 'voz-feminina', @orderA := @orderA + 1, IF(voz = 'voz-masculina', @orderB := @orderB + 1, null)) AS idx
    FROM musica
    WHERE audio = 'pop'
) AS a
ORDER BY idx, voz;
");
$q->execute();
while ($linha = $q->fetch(PDO::FETCH_ASSOC)) {
    echo $linha['voz'];
}

And the error is this:

  

Fatal error: Uncaught exception 'PDOException' with message   'SQLSTATE [HY000]: General error: 2014 Can not 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. ' in   D: \ web \ localuser \ www \ new \ index.php: 15 Stack trace: # 0   D: \ web \ localuser \ www \ new \ index.php (15): PDOStatement-> execute () # 1   {main} thrown in D: \ web \ localuser \ www \ new \ index.php on line 15

What can it be?

    
asked by anonymous 21.08.2015 / 03:04

3 answers

1

Good person. thank you all for your efforts. when I was developing this code in php on local server I used the php version 5.5.12 and although it worked perfectly, my problems started when I uploaded the code to a hosting server with the version of php 5.2.xx ai presented this problem. when I realized the difference of versions I asked for a migration to a server with php version 5.5.12 or higher, and problem solved. I do not know if this was really a solution or a gambiarra. but now everything is running perfectly. Thanks to all stackoverflow programmers, you are grade 10.

    
26.08.2015 / 02:17
0

Try this:

$conn->exec('SET @orderA := 0; SET @orderB := 0;');

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

$q = $conn->prepare("
SELECT id, voz
FROM (
    SELECT id, voz
    ,IF(voz = 'voz-feminina', @orderA := @orderA + 1, IF(voz = 'voz-masculina', @orderB := @orderB + 1, null)) AS idx
    FROM musica
    WHERE audio = 'pop'
) AS a
ORDER BY idx, voz;
");
$q->execute();

$resultados = new ArrayIterator($q->fetchAll(PDO::FETCH_OBJ));

foreach($resultados as $res) {
    echo $res->voz;
}
    
21.08.2015 / 04:07
0

If you have a query before exec() , close it before starting the next one with closeCursor () , this is recommended for multiple queries and the use of fetch() .

$stmt->closeCursor(); //fecha a consulta anterior
$conn->exec('SET @orderA := 0; SET @orderB := 0;');
    
21.08.2015 / 14:17