¿Cómo usar el objeto de retorno PHP PDO en varias ocasiones?

¿Cómo usar el objeto de retorno PHP PDO en varias ocasiones?

As per my below code $result object does not display any data in while loop.

Mi código

try {
    $dbhandle = new PDO("sqlite:".$database);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();

$result=$dbhandle->query("select * from table");


        while ($rs1 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) 
            echo "<pre>";
            echo "</pre>";
        // error message

preguntado el 28 de agosto de 12 a las 13:08

2 Respuestas

This is because the cursor is at the end. So you have to reset the cursor.


Normally in MySQL you can use MySQL Free result.

resultado libre de pdo

contestado el 23 de mayo de 17 a las 12:05

Please give me some suggestion or example about closecursor, i am not getting. - Hkachhia

Close cursor does not seem to reset the cursor to the beginning, allowing more fetch statements. A fetchAll followed by a closeCursor and another fetch returns an empty row. - david grenier

Although not strictly answering your question, why dont you take a look at the $stmt->fetchAll with PDO statements which takes care of the loop for you and returns you the full associative arrays? Saves you a ton of time!

Respondido 28 ago 12, 13:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.