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
c199dc9d
Commit
c199dc9d
authored
Aug 11, 2013
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added advanced where options
parent
548e5ac1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
5 deletions
+68
-5
MysqliDb.php
MysqliDb.php
+46
-5
readme.md
readme.md
+22
-0
No files found.
MysqliDb.php
View file @
c199dc9d
...
...
@@ -331,11 +331,37 @@ class MysqliDb
//Prepair the where portion of the query
$this
->
_query
.=
' WHERE '
;
foreach
(
$this
->
_where
as
$column
=>
$value
)
{
$comparison
=
' = ? '
;
if
(
is_array
(
$value
)
)
{
// if the value is an array, then this isn't a basic = comparison
$key
=
key
(
$value
);
$val
=
$value
[
$key
];
switch
(
strtolower
(
$key
)
)
{
case
'in'
:
$comparison
=
' IN ('
;
foreach
(
$val
as
$v
){
$comparison
.=
' ?,'
;
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$v
);
}
$comparison
=
rtrim
(
$comparison
,
','
)
.
' ) '
;
break
;
case
'between'
:
$comparison
=
' BETWEEN ? AND ? '
;
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$val
[
0
]
);
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$val
[
1
]
);
break
;
default
:
// We are using a comparison operator with only one parameter after it
$comparison
=
' '
.
$key
.
' ? '
;
// Determines what data type the where column is, for binding purposes.
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$val
);
}
}
else
{
// Determines what data type the where column is, for binding purposes.
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$value
);
}
// Prepares the reset of the SQL query.
$this
->
_query
.=
(
$column
.
' = ?
AND '
);
$this
->
_query
.=
(
$column
.
$comparison
.
'
AND '
);
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
' AND '
);
}
...
...
@@ -387,7 +413,22 @@ class MysqliDb
if
(
$this
->
_where
)
{
$this
->
_bindParams
[
0
]
.=
$this
->
_whereTypeList
;
foreach
(
$this
->
_where
as
$prop
=>
$val
)
{
array_push
(
$this
->
_bindParams
,
$this
->
_where
[
$prop
]);
if
(
!
is_array
(
$val
))
{
array_push
(
$this
->
_bindParams
,
$this
->
_where
[
$prop
]);
continue
;
}
// if val is an array, this is not a basic = comparison operator
$key
=
key
(
$val
);
$vals
=
$val
[
$key
];
if
(
is_array
(
$vals
))
{
// if vals is an array, this comparison operator takes more than one parameter
foreach
(
$vals
as
$k
=>
$v
)
{
array_push
(
$this
->
_bindParams
,
$this
->
_where
[
$prop
][
$key
][
$k
]);
}
}
else
{
// otherwise this comparison operator takes only one parameter
array_push
(
$this
->
_bindParams
,
$this
->
_where
[
$prop
][
$key
]);
}
}
}
}
...
...
readme.md
View file @
c199dc9d
...
...
@@ -73,6 +73,7 @@ print_r($results); // contains array of returned rows
### Where Method
This method allows you to specify the parameters of the query.
Regular == operator:
```
php
$db
->
where
(
'id'
,
int
);
$db
->
where
(
'title'
,
string
);
...
...
@@ -80,6 +81,27 @@ $results = $db->get('tableName');
print_r
(
$results
);
// contains array of returned rows
```
Custom Operators:
```
php
$db
->
where
(
'id'
,
array
(
'>='
=>
50
)
);
$results
=
$db
->
get
(
'tableName'
);
// Gives: SELECT * FROM tableName WHERE id >= ?
```
BETWEEN:
```
php
$db
->
where
(
'id'
,
array
(
'between'
=>
array
(
4
,
20
)
)
);
$results
=
$db
->
get
(
'tableName'
);
// Gives: SELECT * FROM tableName WHERE id BETWEEN ? AND ?
```
IN:
``php
$db->where( 'id', array( 'in' => array(1, 5, 27, -1, 'd') ) );
$results = $db->get('tableName');
// Gives: SELECT * FROM tableName WHERE id IN ( ?, ?, ?, ?, ? )
```
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
```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