F
F
Fyodor2014-04-22 07:24:05
PHP
Fyodor, 2014-04-22 07:24:05

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();
        }

}

But at the same time I keep getting the error "Commands out of sync; you can't run this command now". Moreover, if without transactions, everything worked for me.
I cut the code when posting, I hope I didn't mess it up.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question