downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

mysqli::$connect_errno> <mysqli::close
[edit] Last updated: Fri, 24 May 2013

view this page in

mysqli::commit

mysqli_commit

(PHP 5)

mysqli::commit -- mysqli_commitValide la transaction courante

Description

Style orienté objet

bool mysqli::commit ( void )

Style procédural

bool mysqli_commit ( mysqli $link )

Valide la transaction courante pour la base de données spécifiée par le paramètre link.

Liste de paramètres

link

Seulement en style procédural : Un identifiant de lien retourné par la fonction mysqli_connect() ou par la fonction mysqli_init()

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec mysqli::commit()

Style orienté objet

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

/* Vérification de la connexion */
if (mysqli_connect_errno()) {
    
printf("Échec de la connexion : %s\n"mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE Language LIKE CountryLanguage");

/* Désactivation de l'autocommit */
$mysqli->autocommit(FALSE);

/* Insertion de quelques valeurs */
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
$mysqli->query("INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");

/* Validation de la transaction */
$mysqli->commit();

/* Effacement de la table */
$mysqli->query("DROP TABLE Language");

/* Fermeture de la connexion */
$mysqli->close();
?>

Style procédural

<?php
$link 
mysqli_connect("localhost""my_user""my_password""test");

/* Vérification de la connexion */
if (!$link) {
    
printf("Échec de la connexion : %s\n"mysqli_connect_error());
    exit();
}

/* Désactivation de l'autocommit */
mysqli_autocommit($linkFALSE);

mysqli_query($link"CREATE TABLE Language LIKE CountryLanguage");

/* Insertion de quelques valeurs */
mysqli_query($link"INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
mysqli_query($link"INSERT INTO Language VALUES ('DEU', 'Swabian', 'F', 9.4)");

/* Validation de la transaction */
mysqli_commit($link);

/* Fermeture de la connexion */
mysqli_close($link);
?>

Voir aussi



mysqli::$connect_errno> <mysqli::close
[edit] Last updated: Fri, 24 May 2013
 
add a note add a note User Contributed Notes mysqli::commit - [3 notes]
up
1
Lorenzo - webmaster AT 4tour DOT it
4 years ago
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!
up
-2
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
               
}
?>
up
-2
mvanlamz
4 years ago
Please note that calling mysqli::commit() will NOT automatically set mysqli::autocommit() back to 'true'.

This means that any queries following mysqli::commit() will be rolled back when your script exits.

 
show source | credits | sitemap | contact | advertising | mirror sites