如何使用多次PHP PDO返回对象?

How to use multiple time PHP PDO return object?

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

My code

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

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

if($result)
{
    $row=$result->fetch(PDO::FETCH_ASSOC)

    if(count($row)>0)
    {
        while ($rs1 = $result->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) 
        {
            echo "<pre>";
            print_r($rs1);
            echo "</pre>";
        }
    }
    else
    {
        // error message
    }
}

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

http://us.php.net/manual/en/pdostatement.closecursor.php

Normally in MySQL you can use MySQL Free result.

pdo free result

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!