Leonard
Leonard

Reputation: 3092

bind_param and execute in loop?

Is it possible to invoke bind_param and execute iteratively, or must I prepare a statement at the beginning of each iteration?

$query = $db->prepare('...');
foreach ($dataItem as $item) {
    $query->bind_param($v1, $v2, ..., $item);
    $query->execute();
}
$query->close();

If I do have to recreate the statement each iteration, is it possible to optimize this process?

Thank you!

Upvotes: 5

Views: 3041

Answers (1)

hakre
hakre

Reputation: 198118

There is no need to prepare a statement at the beginning of each iteration.

The concept of prepared statements is to reuse the same statement multiple times in the first place, so it's good to go to prepare once and execute it multiple times.

See also this note on the manual page.

Upvotes: 7

Related Questions