PDO Arabelleksiz sorgular

5 Cevap php

Ben PDO ayrıntıları girmek için çalışıyorum. Yani bu kodlu:

$cn = getConnection();

// get table sequence
$comando = "call p_generate_seq('bitacora')";
$id = getValue($cn, $comando);

//$comando = 'INSERT INTO dsa_bitacora (id, estado, fch_creacion) VALUES (?, ?, ?)';
$comando = 'INSERT INTO dsa_bitacora (id, estado, fch_creacion) VALUES (:id, :estado, :fch_creacion)';
$parametros = array (
    ':id'=> (int)$id,
    ':estado'=>1,
    ':fch_creacion'=>date('Y-m-d H:i:s')
);
execWithParameters($cn, $comando, $parametros);

Benim getValue fonksiyonu çalışıyor, ve ben tablo için bir sonraki dizisi olsun. Ben execWithParameters içine almak ama, ben bu istisna olsun:

PDOException: sqlstate [HY000]: Genel hata: diğer tamponsuz sorgular aktif iken 2014 sorguları yürütmek olamaz. PDOStatement :: fetchAll () kullanmayı düşünün. Kod sadece hiç mysql karşı çalıştırmak için gidiyor ise Alternatif olarak, PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY niteliğini ayarlayarak sorgu arabelleğe etkinleştirebilirsiniz. D: satır 77 \ Servidor \ xampp_1_7_1 \ htdocs \ bitacora \ func_db.php

Ben bağlantı özelliklerini değiştirmeye çalıştı ama o çalışmıyor.

Bunlar benim ana db işlevleri şunlardır:

function getConnection() {
    try {
        $cn = new PDO("mysql:host=$host;dbname=$bd", $usuario, $clave, array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            ));

        $cn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
        return $cn;
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}
function getValue($cn, $comando) {
    $resul = $cn->query($comando);
        if (!$resul) return null;
        while($res = $resul->fetch()) {
            $retorno = $res[0][0];
            break;
        }
        return $retorno;
}
function execWithParameters($cn, $comando, $parametros) {
    $q = $cn->prepare($comando);
    $q->execute($parametros);
    if ($q->errorInfo() != null) {
        $e = $q->errorInfo();
        echo $e[0].':'.$e[1].':'.$e[2];
    }
}

Bunun için bir ışık tutabilecek birileri? PD. I başka bir sistemden taşıma am nedeni, autonumeric id yapıyor öneririz etmeyiniz.

5 Cevap

Sorun mysql sadece belirli bir zamanda bir üstün imleç için izin vermesidir. Fetch () yöntemini kullanarak ve bekleyen tüm verileri tüketen değil, açık bir imleç ayrılıyor.

The recommended approach is to consume all the data using the fetchAll() method. An alternative is to use the closeCursor() method.

Bu işlevi değiştirirseniz, ben mutlu olacağını düşünüyorum:

<?php
function getValue($cn, $comando) {
    $resul = $cn->query($comando);
    if (!$resul) return null;
    foreach ($resul->fetchAll() as $res) {
            $retorno = $res[0];
            break;
    }
    return $retorno;
}
?>

Sorun gibi görünüyor --- sizin getValue çağrı döndükten sonra, sorgu hala (Sen sadece ilk değer sormak için henüz bağlantı birkaç döner bağlantısı için bağlı olduğu --- PDO ile çok tanıdık değil Ben, ya) bunu bekliyor.

Belki getValue ekleyerek sabit olabilir

$resul->closeCursor();

dönmeden önce.

GetValue sorgular her zaman tek (ya da yeterince az) değeri dönecektir Aksi takdirde, bu fetchAll kullanarak tercih olacak gibi görünüyor.

Ben veri döndüren bir sorgu (yani bir UPDATE, INSERT, vb) yapıyor değilseniz PDOStatement :: closeCursor () işe yarayacağını sanmıyorum.

Daha iyi bir çözüm PDOStatement çağırarak :: yürütmek sonra basitçe unset () PDOStatement nesnesi ():

$stmt = $pdo->prepare('UPDATE users SET active = 1');
$stmt->execute();
unset($stmt);

Bir arkadaşım xampp 1.7.1 yapı ile çok aynı problem vardı. 5.2.9-2 php.net yapı tarafından xampp / php / * değiştirilmesi ve xampp / apache / bin tüm gerekli dosyaları kopyaladıktan sonra iyi çalıştı.

Eğer XAMPP 1.7.1 kullanıyorsanız, sadece 1.7.2 yükseltmeniz gerekir.