The classic transactions in a loop code:
$mysqli->query("START TRANSACTION");
foreach ($pdata as $key => $value) {
$sql = "INSERT INTO temp (`fund_id`) VALUES (" . $value . ")";
$result = $mysqli->query($sql);
}
$mysqli->query("COMMIT");
Then we change to prepared statements:
$mysqli->autocommit(FALSE);
foreach ($pdata as $key => $value) {
$sql = "INSERT INTO temp (`fund_id`) VALUES (?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i', $value);
$stmt->execute();
}
$mysqli->commit();
Questions:
1) Are these two codes identical? Am I missing something in the second code with prepared statements?
2) Is $mysqli->commit()
the same as $mysqli->query("COMMIT")
?
3) Do I need to add $mysqli->query("START TRANSACTION");
for the prepared statements block or the transaction will automatically start when we set autocommit(FALSE)
?