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
a6f3e051
Commit
a6f3e051
authored
Jun 12, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ups
parent
5e193ce0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
0 deletions
+57
-0
MysqliDb.php
MysqliDb.php
+42
-0
readme.md
readme.md
+15
-0
No files found.
MysqliDb.php
View file @
a6f3e051
...
...
@@ -215,6 +215,48 @@ class MysqliDb
return
$this
->
_dynamicBindResults
(
$stmt
);
}
/**
* Pass in an array of subqueries for union query construction.
*
* @param array $objects Contains a user-provided array of subqueries
* @param $type 'ALL', 'DISTINCT', null.
*
* @return array Contains the returned rows from the query.
*/
public
function
union
(
$objects
,
$type
=
null
)
{
$allowedTypes
=
array
(
'ALL'
,
'DISTINCT'
);
$type
=
strtoupper
(
trim
(
$type
));
if
(
$type
&&
!
in_array
(
$type
,
$allowedTypes
))
die
(
'Wrong UNION type: '
.
$type
);
if
(
!
is_array
(
$objects
))
return
;
$this
->
_query
=
""
;
$i
=
0
;
foreach
(
$objects
as
$obj
)
{
if
(
!
is_object
(
$obj
))
continue
;
if
(
$i
++
!=
0
)
$this
->
_query
.=
" UNION
{
$type
}
"
;
$subQuery
=
$obj
->
getSubQuery
();
$this
->
_query
.=
"("
.
$subQuery
[
'query'
]
.
")"
;
foreach
(
$subQuery
[
'params'
]
as
$v
)
$this
->
_bindParam
(
$v
);
}
$stmt
=
$this
->
_buildQuery
(
null
);
$stmt
->
execute
();
$this
->
_stmtError
=
$stmt
->
error
;
$this
->
reset
();
return
$this
->
_dynamicBindResults
(
$stmt
);
}
/**
*
* @param string $query Contains a user-provided select query.
...
...
readme.md
View file @
a6f3e051
...
...
@@ -260,6 +260,21 @@ $data = Array (
$id
=
$db
->
insert
(
"products"
,
$data
);
// Gives INSERT INTO PRODUCTS (productName, userId, lastUpdated) values ("test product", (SELECT name FROM users WHERE id = 6), NOW());
```
UNION queries
```
php
$common
=
$db
->
subQuery
();
$common
->
where
(
"agentId"
,
10
);
$customers
=
$common
->
copy
();
$customers
->
get
(
"customers"
);
$users
=
$common
->
copy
();
$users
->
get
(
"users"
);
$db
->
orderBy
(
"lastModified"
);
$res
=
$db
->
union
(
Array
(
$customers
,
$users
,
$companies
),
"ALL"
);
GIVES
(
SELECT
*
FROM
customers
WHERE
agentId
=
10
)
UNION
ALL
(
SELECT
*
FROM
users
WHERE
agentId
=
10
)
ORDER
BY
lastModified
DESC
```
### Helper commands
Reconnect in case mysql connection died
```
php
...
...
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