【问题标题】:Reset row pointer of a mySQL result (PDO) [duplicate]重置 mySQL 结果的行指针 (PDO) [重复]
【发布时间】:2013-10-11 21:03:53
【问题描述】:

我需要循环几次查询的结果。在第一个循环之后,由于指针位于底部,我无法再次查看结果。我怎样才能让它回到顶部?我不想再次运行查询,因为这只会消耗更多资源并返回相同的结果。

$stmt = $conn->prepare("SELECT * FROM customers");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();

//This kind of loop will repeat elsewhere in the code
while($row = $stmt->fetch()){ 
//Do something here
}

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:
    while($row = $stmt->fetch()){ 
        $rows[] = $row;  //use $rows later
        //Do something here with $row
    }
    

    【讨论】:

    • 刚看到重复。只需使用 $rows = $stmt->fetchAll();
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多