Answer the question
In order to leave comments, you need to log in
How to make transactions in mysqli with multi_query?
I have an import script, a lot of data, I read that I need to use transactions, otherwise everything is extremely long and loaded.
Actually, when I try to use, I follow the examples
$mysqli->real_query("SELECT * FROM sd_hotellook_cityids WHERE countryid != 0 AND updated = 0 AND checked = 0 ORDER by id LIMIT 3");
$query = $mysqli->store_result();
$mysqli->autocommit(FALSE);
while ($city = $query->fetch_assoc()) {
// здесь собираются все запросы в sql переменную для каждого города
$sql .= ("INSERT sd_hotellook_hotels_shortFacilities (id, name,cityid) VALUES $shortFacilities; ");
//и потом выполняем
if (!empty($sql)) {
if ($mysqli->multi_query($sql)) {
do {
// fetch results
if (!$mysqli->more_results()) {
break;
}
if (!$mysqli->next_result()) {
echo 'Insert Error: ' . $mysqli->error . '<br />';
$error = 1;
break;
}
} while (true);
}
}
if( $error != 1){
if (!$mysqli->commit())
{
echo 'Insert Error: ' . $mysqli->error . '<br />';
}
}else{
$mysqli->rollback();
exit();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question