I have a script that inserts an array of rows into a database, where each row is represented by a subarray. Recently, I realized that I was using prepared statements in PHP/MySQLi incorrectly, as I was re-preparing the same statement as I iterated through the big array. I then converted the code to the following:
$t = reset($data);
$stmt = $con->prepare("INSERT INTO tlist VALUES (?,?,?)");
$stmt->bind_param('isi',$t['id'],$t['flag'],$t['cost']);
foreach ($data as $t) {
$stmt->execute();
}
$stmt->close();
$con->close();
However, this now attempts to insert the values in the first sub-array of $t
every iteration of the loop. What am I doing wrong here? Is there a way to retain the increased performance of the prepared statement without having to manually assign the subarrays' values into standard variables first?
EDIT: try to remove duplicate tag