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
7cd05ca5
Commit
7cd05ca5
authored
Nov 18, 2016
by
Alexander Butenko
Committed by
GitHub
Nov 18, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7 from joshcam/master
q
parents
64b5a57b
8305412a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
124 additions
and
4 deletions
+124
-4
MysqliDb.php
MysqliDb.php
+42
-0
index.php
index.php
+1
-1
readme.md
readme.md
+38
-1
mysqliDbTests.php
tests/mysqliDbTests.php
+43
-2
No files found.
MysqliDb.php
View file @
7cd05ca5
...
...
@@ -679,6 +679,48 @@ class MysqliDb
return
$this
->
_buildInsert
(
$tableName
,
$insertData
,
'INSERT'
);
}
/**
* Insert method to add several rows at once
*
* @param string $tableName The name of the table.
* @param array $multiInsertData Two-dimensinal Data-array containing information for inserting into the DB.
* @param array $dataKeys Optinal Table Key names, if not set in insertDataSet.
*
* @return bool|array Boolean indicating the insertion failed (false), else return id-array ([int])
*/
public
function
insertMulti
(
$tableName
,
array
$multiInsertData
,
array
$dataKeys
=
null
)
{
// only auto-commit our inserts, if no transaction is currently running
$autoCommit
=
(
isset
(
$this
->
_transaction_in_progress
)
?
!
$this
->
_transaction_in_progress
:
true
);
$ids
=
array
();
if
(
$autoCommit
)
{
$this
->
startTransaction
();
}
foreach
(
$multiInsertData
as
$insertData
)
{
if
(
$dataKeys
!==
null
)
{
// apply column-names if given, else assume they're already given in the data
$insertData
=
array_combine
(
$dataKeys
,
$insertData
);
}
$id
=
$this
->
insert
(
$tableName
,
$insertData
);
if
(
!
$id
)
{
if
(
$autoCommit
)
{
$this
->
rollback
();
}
return
false
;
}
$ids
[]
=
$id
;
}
if
(
$autoCommit
)
{
$this
->
commit
();
}
return
$ids
;
}
/**
* Replace method to add new row
*
...
...
index.php
View file @
7cd05ca5
...
...
@@ -118,7 +118,7 @@ if ($_GET) {
<input
type=
text
name=
'lastName'
required
placeholder=
'Last Name'
value=
'
<?php
echo
$data
[
'lastName'
]
?>
'
>
<input
type=
password
name=
'password'
placeholder=
'Password'
>
<input
type=
submit
value=
'New User'
></td>
<form>
<
/
form>
</table>
</center>
</body>
...
...
readme.md
View file @
7cd05ca5
...
...
@@ -144,6 +144,43 @@ $db->onDuplicate($updateColumns, $lastInsertId);
$id
=
$db
->
insert
(
'users'
,
$data
);
```
Insert multiple datasets at once
```
php
$data
=
Array
(
Array
(
"login"
=>
"admin"
,
"firstName"
=>
"John"
,
"lastName"
=>
'Doe'
),
Array
(
"login"
=>
"other"
,
"firstName"
=>
"Another"
,
"lastName"
=>
'User'
,
"password"
=>
"very_cool_hash"
)
);
$ids
=
$db
->
insertMulti
(
'users'
,
$data
);
if
(
!
$ids
)
{
echo
'insert failed: '
.
$db
->
getLastError
();
}
else
{
echo
'new users inserted with following id\'s: '
.
implode
(
', '
,
$ids
);
}
```
If all datasets only have the same keys, it can be simplified
```
php
$data
=
Array
(
Array
(
"admin"
,
"John"
,
"Doe"
),
Array
(
"other"
,
"Another"
,
"User"
)
);
$keys
=
Array
(
"login"
,
"firstName"
,
"lastName"
);
$ids
=
$db
->
insertMulti
(
'users'
,
$data
,
$keys
);
if
(
!
$ids
)
{
echo
'insert failed: '
.
$db
->
getLastError
();
}
else
{
echo
'new users inserted with following id\'s: '
.
implode
(
', '
,
$ids
);
}
```
### Replace Query
<a
href=
'https://dev.mysql.com/doc/refman/5.0/en/replace.html'
>
Replace()
</a>
method implements same API as insert();
...
...
@@ -713,7 +750,7 @@ else
echo 'Update failed. Error: '. $db->getLastError();
```
### Query exec
t
ution time benchmarking
### Query execution time benchmarking
To track query execution time setTrace() function should be called.
```php
$db->setTrace (true);
...
...
tests/mysqliDbTests.php
View file @
7cd05ca5
...
...
@@ -396,8 +396,8 @@ if ($c != 3) {
}
unset
(
$cnt
);
$
data
=
$db
->
get
(
'users'
);
if
(
count
(
$
data
)
!=
3
)
{
$
users
=
$db
->
get
(
'users'
);
if
(
count
(
$
users
)
!=
3
)
{
echo
"copy with subquery data count failed"
;
exit
;
}
...
...
@@ -444,6 +444,47 @@ if (key ($result) != 1 && !is_object ($result[1])) {
exit
;
}
$expectedIDs
=
[
'users'
=>
[
5
,
6
,
7
],
'products'
=>
[
6
,
7
,
8
,
9
,
10
],
];
// multi-insert test with autoincrement
foreach
(
$data
as
$name
=>
$datas
)
{
// remove previous entries to ensure avoiding PRIMARY-KEY collisions here
$db
->
delete
(
$name
);
// actual insertion test
$ids
=
$db
->
insertMulti
(
$name
,
$datas
);
// check results
if
(
!
$ids
)
{
echo
"failed to multi-insert: "
.
$db
->
getLastQuery
()
.
"
\n
"
.
$db
->
getLastError
();
exit
;
}
elseif
(
$ids
!==
$expectedIDs
[
$name
])
{
pretty_print
(
$ids
);
echo
"multi-insert succeeded, but unexpected id's: "
.
$db
->
getLastQuery
()
.
"
\n
"
.
$db
->
getLastError
();
exit
;
}
}
// skip last user here, since it has other keys than the others
unset
(
$data
[
'users'
][
2
]);
// multi-insert test with autoincrement and overriding column-names
foreach
(
$data
as
$name
=>
$datas
)
{
// remove previous entries to ensure avoiding PRIMARY-KEY collisions here
$db
->
delete
(
$name
);
// actual insertion test
if
(
!
$db
->
insertMulti
(
$name
,
$datas
,
array_keys
(
$datas
[
0
])))
{
echo
"failed to multi-insert: "
.
$db
->
getLastQuery
()
.
"
\n
"
.
$db
->
getLastError
();
exit
;
}
}
///
//TODO: insert test
$db
->
delete
(
"users"
);
...
...
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