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
dd7376b4
Commit
dd7376b4
authored
Mar 31, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement orWhere condition
parent
b56cb825
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
18 deletions
+37
-18
MysqliDb.php
MysqliDb.php
+29
-18
readme.md
readme.md
+8
-0
No files found.
MysqliDb.php
View file @
dd7376b4
...
...
@@ -286,7 +286,7 @@ class MysqliDb
}
/**
* This method allows you to specify multipl
(method chaining optional)
WHERE statements for SQL queries.
* This method allows you to specify multipl
e (method chaining optional) AND
WHERE statements for SQL queries.
*
* @uses $MySqliDb->where('id', 7)->where('title', 'MyTitle');
*
...
...
@@ -297,10 +297,25 @@ class MysqliDb
*/
public
function
where
(
$whereProp
,
$whereValue
)
{
$this
->
_where
[
$whereProp
]
=
$whereValue
;
$this
->
_where
[
$whereProp
]
=
Array
(
"AND"
,
$whereValue
)
;
return
$this
;
}
/**
* This method allows you to specify multiple (method chaining optional) OR WHERE statements for SQL queries.
*
* @uses $MySqliDb->orWhere('id', 7)->orWhere('title', 'MyTitle');
*
* @param string $whereProp The name of the database field.
* @param mixed $whereValue The value of the database field.
*
* @return MysqliDb
*/
public
function
orWhere
(
$whereProp
,
$whereValue
)
{
$this
->
_where
[
$whereProp
]
=
Array
(
"OR"
,
$whereValue
);
return
$this
;
}
/**
* This method allows you to concatenate joins for the final SQL statement.
*
...
...
@@ -477,6 +492,12 @@ class MysqliDb
//Prepair the where portion of the query
$this
->
_query
.=
' WHERE '
;
foreach
(
$this
->
_where
as
$column
=>
$value
)
{
$andOr
=
''
;
// Determine if where condition was a first one or it was AND or OR type
if
(
array_search
(
$column
,
array_keys
(
$this
->
_where
))
!=
0
)
$andOr
=
' '
.
$value
[
0
]
.
' '
;
$value
=
$value
[
1
];
$comparison
=
' = ? '
;
if
(
is_array
(
$value
)
)
{
// if the value is an array, then this isn't a basic = comparison
...
...
@@ -507,9 +528,8 @@ class MysqliDb
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$value
);
}
// Prepares the reset of the SQL query.
$this
->
_query
.=
(
$
column
.
$comparison
.
' AND '
);
$this
->
_query
.=
(
$
andOr
.
$column
.
$comparison
);
}
$this
->
_query
=
rtrim
(
$this
->
_query
,
' AND '
);
}
// Did the user call the "groupBy" method?
...
...
@@ -598,22 +618,13 @@ class MysqliDb
if
(
$hasConditional
)
{
if
(
$this
->
_where
)
{
$this
->
_bindParams
[
0
]
.=
$this
->
_whereTypeList
;
foreach
(
$this
->
_where
as
$prop
=>
$val
)
{
foreach
(
$this
->
_where
as
$val
)
{
$val
=
$val
[
1
];
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
]);
}
array_push
(
$this
->
_bindParams
,
$val
);
}
else
{
// otherwise this comparison operator takes only one parameter
array_push
(
$this
->
_bindParams
,
$this
->
_where
[
$prop
][
$key
]
);
foreach
(
$val
as
$v
)
array_push
(
$this
->
_bindParams
,
$v
);
}
}
}
...
...
readme.md
View file @
dd7376b4
...
...
@@ -145,6 +145,14 @@ $results = $db->get('users');
// Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd');
```
OR CASE
```
php
$db
->
where
(
'firstName'
,
'John'
);
$db
->
orWhere
(
'firstName'
,
'Peter'
);
$results
=
$db
->
get
(
'users'
);
// Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter'
```
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