note 123851 added to mysqli.commit

From: Date: Wed, 15 May 2019 14:23:14 +0000
Subject: note 123851 added to mysqli.commit
Groups: php.notes 
Request: Send a blank email to php-notes+get-211048@lists.php.net to get a copy of this message
When you have alot of transactions to make, say you are applying inserting items to the database from a loop, it will be better to use the mysqli_commit for this kind of process as it will only hit the database once. //Wrong way Example 1: $con = mysqli_connect("host", "username", "password", "database") or die("Could not establish connection to database"); $users = ["chris", "james", "peter", "mark", "joe", "alice", "bob"] for($i=0; $i<count($users); $i++){ $user= $users[$i]; $query = mysqli_query($con, "INSERT INTO users (username) VALUES ('$user') "); } //Correct Way Example 2 $con = mysqli_connect("host", "username", "password", "database") or die("Could not establish connection to database"); $users = ["chris", "james", "peter", "mark", "joe", "alice", "bob"] //Turn off autocommit mysqli_autocommit($con, FALSE) //Make some transactions for($i=0; $i<count($users); $i++){ $user= $users[$i]; $query = mysqli_query($con, "INSERT INTO users (username) VALUES ('$user') "); } //Make a one-time hit to the database mysqli_commit($con) As with the Example 1, since we had 7 items in the list, this means that their will be a 7 times hit to our database which can really affect performance. But with the Example 2, since we already turned off autocommit this means that the transactions will be queued ontill will explicitly call mysqli_commit($con) ---- Server IP: 208.43.231.9 Probable Submitter: 82.199.130.26 (proxied: 160.152.14.220) ---- Manual Page -- http://php.net/manual/en/mysqli.commit.php Edit -- https://master.php.net/note/edit/123851 Del: integrated -- https://master.php.net/note/delete/123851/integrated Del: useless -- https://master.php.net/note/delete/123851/useless Del: bad code -- https://master.php.net/note/delete/123851/bad+code Del: spam -- https://master.php.net/note/delete/123851/spam Del: non-english -- https://master.php.net/note/delete/123851/non-english Del: in docs -- https://master.php.net/note/delete/123851/in+docs Del: other reasons-- https://master.php.net/note/delete/123851 Reject -- https://master.php.net/note/reject/123851 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211048) next »