query(„CREATE TABLE IF NOT EXISTS language ( Code text NOT NULL, Speakers int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;“); /* Turn autocommit off */ $mysqli->autocommit(false); $result = $mysqli->query(„SELECT @@autocommit“); $row = $result->fetch_row(); printf(„Autocommit is %sn“, $row[0]); try { /* Prepare insert statement */ $stmt = $mysqli->prepare(‘INSERT INTO language(Code, Speakers) VALUES (?,?)’); $stmt->bind_param(‘ss’, $language_code, $native_speakers); /* Insert some values */ $language_code = ‘DE’; $native_speakers = 50_123_456; $stmt->execute(); $language_code = ‘FR’; $native_speakers = 40_546_321; $stmt->execute(); /* Commit the data in the database. This doesn’t set autocommit=true */ $mysqli->commit(); print „Committed 2 rows in the databasen“; $result = $mysqli->query(„SELECT @@autocommit“); $row = $result->fetch_row(); printf(„Autocommit is %sn“, $row[0]); /* Try to insert more values */ $language_code = ‘PL’; $native_speakers = 30_555_444; $stmt->execute(); $language_code = ‘DK’; $native_speakers = 5_222_444; $stmt->execute(); /* Setting autocommit=true will trigger a commit */ $mysqli->autocommit(true); print „Committed 2 row in the databasen“; } catch (mysqli_sql_exception $exception) { $mysqli->rollback(); throw $exception; } The above examples will output:
Autocommit is 0 Committed 2 rows in the database Autocommit is 0 Committed 2 row in the database Autocommit is 0 Committed 2 rows in the database Autocommit is 0 Committed 2 row in the database