Bruce Bruce -4 years ago 127
MySQL Question

How to batch a pdo loop

I am completely stumped how to do this. I want to perform a function per batch of 100 mysql results.

$stmt=$db->prepare("SELECT count(user) AS cnt, user FROM table");
$stmt->execute();
$row = $stmt->fetchAll();
foreach ($row AS $row) {
$user .= $row['user'];
}

function($user);


The trouble is I can't figure out how to stop the loop each 100, perform the function, then continue again until there are no more results.

Answer Source
$stmt=$db->query("SELECT user FROM table");
$i = 1;
foreach ($stmt as $row) {
    $user .= $row['user'];
    if ($i++ == 100) {
        function($user);
        $i = 1;
    }
}
function($user);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download