phppdo

Php PDO | how to use fetchAll with FETCH_LAZY?


I am trying to use FETCH_LAZY. I can use a while loop whilst setting $row equal to $query->fetch(). The problem is that fetchAll does not work when the fetchMode is FETCH_LAZY.


How can I return an array of PDO Objects?

Thanks.


Solution

  • Due to the very nature of FETCH_LAZY, you cannot use fetchAll with this mode. It can be used with fetch() only.

    It seems that you are simply confusing this mode with FETCH_OBJ which will give you an array of objects you need

    $array = $stmt->fetchAll(PDO::FETCH_OBJ);
    

    Also note that beside creating stdObj instances you can make fetchAll to return an array of objects of any other class as well.