Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind_param and execute in loop?

Tags:

php

mysqli

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!

like image 949
Leonard Avatar asked Jul 24 '11 11:07

Leonard


1 Answers

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.

like image 110
hakre Avatar answered Sep 26 '22 01:09

hakre