This is an example to explain the powerful of the rollback and commit functions.
Let's suppose you want to be sure that all queries have to be executed without errors before writing data on the database.
Here's the code:
<?php
$all_query_ok=true; // our control variable
//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE
//now let's test our control variable
$all_query_ok ? $mysqli->commit() : $mysqli->rollback();
$mysqli->close();
?>
hope to be helpful!
mysqli::commit
mysqli_commit
(PHP 5)
mysqli::commit -- mysqli_commit — Realiza un "commit" de la transacción actual
Descripción
Estilo orientado a objetos
bool mysqli::commit
( void
)
Estilo por procedimientos
Realiza un "commit" de la transacción actual para la conexión a la base de datos.
Parámetros
-
link -
Sólo estilo por procediminetos: Un identificador de enlace devuelto por mysqli_connect() o mysqli_init()
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Ejemplos
Ejemplo #1 mysqli::commit() example
Estilo orientado a objetos
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* comprueba la conexión */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE Language LIKE CountryLanguage");
/* Desactiva autocommit */
$mysqli->autocommit(FALSE);
/* Inserta algunos valores */
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");
/* commit transaction */
$mysqli->commit();
/* Elimina la tabla */
$mysqli->query("DROP TABLE Language");
/* Cierra la conexión */
$mysqli->close();
?>
Estilo por procedimientos
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "test");
/* Comprueba la conexión */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Desactiva autocommit */
mysqli_autocommit($link, FALSE);
mysqli_query($link, "CREATE TABLE Language LIKE CountryLanguage");
/* Inserta algunos valores */
mysqli_query($link, "INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
mysqli_query($link, "INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");
/* commit transaction */
mysqli_commit($link);
/* Cierra la conexión */
mysqli_close($link);
?>
Ver también
- mysqli_autocommit() - Activa o desactiva las modificaciones de la base de datos autoconsignadas
- mysqli_rollback() - Revierte la transacción actual
Lorenzo - webmaster AT 4tour DOT it ¶
4 years ago
Bob Johnson ¶
3 years ago
The compactness of Lorenzo's code is admirable.
However, it is a good idea to also check $mysqli->affected_rows to make sure that the INSERT statement did not fail.
<?php
$result_query = @mysqli_query($query, $connect);
if (($result_query == false) &&
(mysqli_affected_rows($connect) == 0))
{
// verify the query executed completely and verify that it
// had impact on the table
$success = false;
// here also, the developer could choose to add a ROLLBACK
// statement
}
?>
