0

If this works..

$stmt = DB::$db->query('SELECT * FROM users');
while ($r = $stmt->fetch()) { print_r($r); }

Is there any way possible to get something like this to work?

function fetch() {
    $stmt = DB::$db->query('SELECT * FROM users');
    return $stmt->fetch();
}
while ($r = fetch()) { print_r($r); }

UPDATE

Or maybe if it's in a class it can work by using some sort of class iterator extension/implementation?

class User {
    public static function fetch() {
        $stmt = DB::$db->query('SELECT * FROM users');
        return $stmt->fetch();
    }
}
while ($r = User::fetch()) { print_r($r); }

SOLUTION

Well I did end up finding a working solution, but the accepted answer which uses a generator is probably the better way to go.

class User {
    private static $stmt;
    public static function fetch() {
        if (static::$stmt === null) {
            static::$stmt = DB::$db->query('SELECT * FROM users');
        }
        return static::$stmt->fetch();
    }
}
while ($r = User::fetch()) { print_r($r); }
3
  • php.net/manual/en/language.generators.overview.php Commented Mar 6, 2018 at 18:00
  • your solution is extremely clumsy and error prone Commented Mar 7, 2018 at 7:48
  • I'm sure it is which is why I said I'd recommend the accepted answer over that solution Commented Mar 7, 2018 at 14:29

2 Answers 2

4

This would do it:

function fetch() {
    $stmt = DB::$db->query('SELECT * FROM users');
    while($row = $stmt->fetch()) { yield $row; }
}
foreach (fetch() as $r) { print_r($r); }

Notes:

  • It will only do the query once.
  • The yield is php 5.5+.
  • Use a foreach on your fetch() mini function.

The reason your initial attempt would not work, is the action just keeps looping indefinitely. Each call to your original fetch() its doing the query each call, and thus returning the first result. This will make the while just go on and on.

Sign up to request clarification or add additional context in comments.

8 Comments

Good example of generators!
I was really hoping to get it working with a while loop ran outside of the function. If I use a foreach then even this will work function fetch() { return DB::$db->query('SELECT * FROM users'); }
@Rick this answer DOES show you how to use while outside of the function
Well the title does say "in while loop".. Sorry if that was confusing.. I'm also updating the question by adding a version that uses a class. Perhaps some sort of class iterator extension/implementation can be used to make it work..
Oh well. I thought it actually is better to use the while and the generator, instead of just return it all. Despite the foreach. But... PDO, and all that. Its already pretty optimized.
|
2

if you don't mid foreach instead of while

function fetch() {
    return DB::$db->query('SELECT * FROM users');
}
foreach (fetch() as $r) { print_r($r); }

1 Comment

This is just simply elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.