Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
9e785639
Commit
9e785639
authored
Jun 19, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added transaction helpers
parent
4f93e793
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
0 deletions
+64
-0
MysqliDb.php
MysqliDb.php
+49
-0
readme.md
readme.md
+15
-0
No files found.
MysqliDb.php
View file @
9e785639
...
@@ -943,4 +943,53 @@ class MysqliDb
...
@@ -943,4 +943,53 @@ class MysqliDb
return
clone
$this
;
return
clone
$this
;
}
}
/**
* Begin a transaction
*
* @uses mysqli->autocommit(false)
* @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
*/
public
function
startTransaction
()
{
$this
->
_mysqli
->
autocommit
(
false
);
$this
->
_transaction_in_progress
=
true
;
register_shutdown_function
(
array
(
$this
,
"_transaction_status_check"
));
}
/**
* Transaction commit
*
* @uses mysqli->commit();
* @uses mysqli->autocommit(true);
*/
public
function
commit
()
{
$this
->
_mysqli
->
commit
();
$this
->
_transaction_in_progress
=
false
;
$this
->
_mysqli
->
autocommit
(
true
);
}
/**
* Transaction rollback function
*
* @uses mysqli->rollback();
* @uses mysqli->autocommit(true);
*/
public
function
rollback
()
{
$this
->
_mysqli
->
rollback
();
$this
->
_transaction_in_progress
=
false
;
$this
->
_mysqli
->
autocommit
(
true
);
}
/**
* Shutdown handler to rollback uncommited operations in order to keep
* atomic operations sane.
*
* @uses mysqli->rollback();
*/
public
function
_transaction_status_check
()
{
if
(
!
$this
->
_transaction_in_progress
)
return
;
echo
"rolling all back"
;
$this
->
rollback
();
}
}
// END class
}
// END class
readme.md
View file @
9e785639
...
@@ -278,3 +278,18 @@ Please note that function returns SQL query only for debugging purposes as its e
...
@@ -278,3 +278,18 @@ Please note that function returns SQL query only for debugging purposes as its e
$db
->
get
(
'users'
);
$db
->
get
(
'users'
);
echo
"Last executed query was "
.
$db
->
getLastQuery
();
echo
"Last executed query was "
.
$db
->
getLastQuery
();
```
```
### Transaction helpers
Please keep in mind that transactions are working on innoDB tables.
Rollback transaction if insert fails:
```
php
$db
->
startTransaction
();
...
if
(
!
$db
->
insert
(
'myTable'
,
$insertData
))
{
//Error while saving, cancel new record
$db
->
rollback
();
}
else
{
//OK
$db
->
commit
();
}
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment