mysqli::begin_transaction

mysqli_begin_transaction

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

mysqli::begin_transaction -- mysqli_begin_transactionStarts a transaction

Beschreibung

Objektorientierter Stil

public mysqli::begin_transaction(int $flags = 0, ?string $name = null): bool

Prozeduraler Stil:

mysqli_begin_transaction(mysqli $mysql, int $flags = 0, ?string $name = null): bool

Begins a transaction. Requires the InnoDB engine (it is enabled by default). For additional details about how MySQL transactions work, see » http://dev.mysql.com/doc/mysql/en/commit.html.

Parameter-Liste

mysql

Nur bei prozeduralem Aufruf: Ein von mysqli_connect() oder mysqli_init() zurückgegebenes mysqli-Objekt.

flags

Valid flags are:

  • MYSQLI_TRANS_START_READ_ONLY: Start the transaction as "START TRANSACTION READ ONLY". Requires MySQL 5.6 and above.

  • MYSQLI_TRANS_START_READ_WRITE: Start the transaction as "START TRANSACTION READ WRITE". Requires MySQL 5.6 and above.

  • MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT: Start the transaction as "START TRANSACTION WITH CONSISTENT SNAPSHOT".

name

Savepoint name for the transaction.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Changelog

Version Beschreibung
8.0.0 name is now nullable.

Beispiele

Beispiel #1 mysqli::begin_transaction() example

Objektorientierter Stil

<?php

/* Tell mysqli to throw an exception if an error occurs */
mysqli_report(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);

$mysqli = new mysqli("localhost""my_user""my_password""world");

/* The table engine has to support transactions */
$mysqli->query("CREATE TABLE IF NOT EXISTS language (
    Code text NOT NULL,
    Speakers int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
);

/* Start transaction */
$mysqli->begin_transaction();

try {
    
/* Insert some values */
    
$mysqli->query("INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");

    
/* Try to insert invalid values */
    
$language_code 'FR';
    
$native_speakers 'Unknown';
    
$stmt $mysqli->prepare('INSERT INTO language(Code, Speakers) VALUES (?,?)');
    
$stmt->bind_param('ss'$language_code$native_speakers);
    
$stmt->execute();

    
/* If code reaches this point without errors then commit the data in the database */
    
$mysqli->commit();
} catch (
mysqli_sql_exception $exception) {
    
$mysqli->rollback();

    throw 
$exception;
}

Prozeduraler Stil

<?php

/* Tell mysqli to throw an exception if an error occurs */
mysqli_report(MYSQLI_REPORT_ERROR MYSQLI_REPORT_STRICT);

$mysqli mysqli_connect("localhost""my_user""my_password""world");

/* The table engine has to support transactions */
mysqli_query($mysqli"CREATE TABLE IF NOT EXISTS language (
    Code text NOT NULL,
    Speakers int(11) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
);

/* Start transaction */
mysqli_begin_transaction($mysqli);

try {
    
/* Insert some values */
    
mysqli_query($mysqli"INSERT INTO language(Code, Speakers) VALUES ('DE', 42000123)");

    
/* Try to insert invalid values */
    
$language_code 'FR';
    
$native_speakers 'Unknown';
    
$stmt mysqli_prepare($mysqli'INSERT INTO language(Code, Speakers) VALUES (?,?)');
    
mysqli_stmt_bind_param($stmt'ss'$language_code$native_speakers);
    
mysqli_stmt_execute($stmt);

    
/* If code reaches this point without errors then commit the data in the database */
    
mysqli_commit($mysqli);
} catch (
mysqli_sql_exception $exception) {
    
mysqli_rollback($mysqli);

    throw 
$exception;
}

Anmerkungen

Hinweis:

This function does not work with non transactional table types (like MyISAM or ISAM).

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Daten einer Abfrage mit Hilfe von Thumbnails nebeneinander ausgeben

Da ich viele unterschiedliche Tabellen benötige mache ich das so umständlich. Aber ist das der Grund warum das Floaten nicht funktioniert?

Geschrieben von Malchor am 17.05.2024 17:15:03
Forum: PHP Developer Forum
Daten einer Abfrage mit Hilfe von Thumbnails nebeneinander ausgeben

Du möchtest dir bestimmt mal ansehen, wie eine HTML-Tabelle richtig aufgebaut wird: https://www.w3schools.com/tags/tag_th.asp align solltest du ...

Geschrieben von scatello am 17.05.2024 17:12:34
Forum: PHP Developer Forum
Daten einer Abfrage mit Hilfe von Thumbnails nebeneinander ausgeben

Guten Tag zusammen, ich habe hier schon mal geschaut, bin aber Tatsache nicht schlau geworden. Ich möchte gerne Daten aus der DB abfragen und d ...

Geschrieben von Malchor am 17.05.2024 16:59:31
Forum: PHP Developer Forum
Buch über PHP, CSS und MySQL

As technology advances, so do the features of TPE sex dolls (https://www.bestrealdoll.com/collections/tpe-sex-dolls). Manufacturers now utilize st ...

Geschrieben von Nancyfrankflower am 17.05.2024 11:21:03
Forum: Bücher-Forum