Reset cursor position in PDO

前端 未结 1 609
孤城傲影
孤城傲影 2020-12-02 02:13
$data=$stmt->fetchAll(); //Dumping the data shows the result. It is also setting the cursor at the end

while($data=$stmt->fetch())
{
//Does not enters loop
//         


        
相关标签:
1条回答
  • 2020-12-02 03:00

    AFAIK there is no possibility to reset cursor position with PDO - that might something to do with compatibility with some databases, that don't support resetting internal cursors.

    If you want to iterate twice over the results, fetch it to the array and iterate over this array:

    <?php 
    $results = $stmt->fetchAll();  
    foreach($results as $row) {
        // first
    }
    
    foreach($results as $row) {
        // second
    }
    

    Edit Some databases support scrollable cursors. To use that, add PDO::CURSOR_SCROLL flag to prepare method (see examples at PDOFetch documentation page). But that only adds possibility to move forward or backward, not rewind completely. Also, not all databases support that type of cursor (e.g. MySQL doesn't).

    0 讨论(0)
提交回复
热议问题