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
19abf864
Commit
19abf864
authored
Aug 03, 2015
by
rongzhj
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add on dulplicate update
parent
c28a3d24
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
MysqliDb.php
MysqliDb.php
+51
-0
No files found.
MysqliDb.php
View file @
19abf864
...
@@ -112,6 +112,18 @@ class MysqliDb
...
@@ -112,6 +112,18 @@ class MysqliDb
*/
*/
protected
$isSubQuery
=
false
;
protected
$isSubQuery
=
false
;
/**
* Name of the auto increment column
*
*/
protected
$lastInsertId
=
null
;
/**
* Column names for update when using onDuplicate method
*
*/
protected
$updateColumns
=
null
;
/**
/**
* Return type: 'Array' to return results as array, 'Object' as object
* Return type: 'Array' to return results as array, 'Object' as object
* 'Json' as json string
* 'Json' as json string
...
@@ -550,6 +562,19 @@ class MysqliDb
...
@@ -550,6 +562,19 @@ class MysqliDb
return
$this
;
return
$this
;
}
}
/**
* This function store update column's name and column name of the
* autoincrement column
*
* @param Array Variable with values
* @param String Variable value
*/
public
function
onDuplicate
(
$updateColumns
,
$lastInsertId
=
null
)
{
$this
->
lastInsertId
=
$lastInsertId
;
$this
->
updateColumns
=
$updateColumns
;
}
/**
/**
* This method allows you to specify multiple (method chaining optional) OR WHERE statements for SQL queries.
* This method allows you to specify multiple (method chaining optional) OR WHERE statements for SQL queries.
*
*
...
@@ -779,6 +804,31 @@ class MysqliDb
...
@@ -779,6 +804,31 @@ class MysqliDb
return
true
;
return
true
;
}
}
/**
* Helper function to add variables into the query statement
*
* @param Array Variable with values
*/
protected
function
_buildDuplicate
(
$tableData
)
{
if
(
is_array
(
$this
->
updateColumns
)
&&
!
empty
(
$this
->
updateColumns
))
{
$this
->
_query
.=
" on duplicate key update "
;
if
(
$this
->
lastInsertId
)
{
$this
->
_lastQuery
.=
$this
->
lastInsertId
.
"=LAST_INSERT_ID("
.
$this
->
lastInsertId
.
"),"
;
$this
->
lastInsertId
=
null
;
}
foreach
(
$this
->
updateColumns
as
$value
)
{
$this
->
_bindParam
(
$tableData
[
$value
]);
$this
->
_query
.=
"`"
.
$value
.
"` = ?, "
;
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
', '
);
$this
->
lastInsertId
=
null
;
$this
->
updateColumns
=
null
;
}
}
/**
/**
* Abstraction method that will compile the WHERE statement,
* Abstraction method that will compile the WHERE statement,
* any passed update data, and the desired rows.
* any passed update data, and the desired rows.
...
@@ -798,6 +848,7 @@ class MysqliDb
...
@@ -798,6 +848,7 @@ class MysqliDb
$this
->
_buildGroupBy
();
$this
->
_buildGroupBy
();
$this
->
_buildOrderBy
();
$this
->
_buildOrderBy
();
$this
->
_buildLimit
(
$numRows
);
$this
->
_buildLimit
(
$numRows
);
$this
->
_buildDuplicate
(
$tableData
);
$this
->
_lastQuery
=
$this
->
replacePlaceHolders
(
$this
->
_query
,
$this
->
_bindParams
);
$this
->
_lastQuery
=
$this
->
replacePlaceHolders
(
$this
->
_query
,
$this
->
_bindParams
);
...
...
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