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
0a03d83b
Commit
0a03d83b
authored
May 19, 2014
by
screeper
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1 from avbdr/master
Merge from avbdr
parents
231fa1dc
fee1b126
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
344 additions
and
100 deletions
+344
-100
MysqliDb.php
MysqliDb.php
+99
-49
index.php
index.php
+110
-12
readme.md
readme.md
+35
-7
tests.php
tests.php
+100
-32
No files found.
MysqliDb.php
View file @
0a03d83b
...
@@ -6,9 +6,10 @@
...
@@ -6,9 +6,10 @@
* @package MysqliDb
* @package MysqliDb
* @author Jeffery Way <jeffrey@jeffrey-way.com>
* @author Jeffery Way <jeffrey@jeffrey-way.com>
* @author Josh Campbell <jcampbell@ajillion.com>
* @author Josh Campbell <jcampbell@ajillion.com>
* @author Alexander V. Butenko <a.butenka@gmail.com>
* @copyright Copyright (c) 2010
* @copyright Copyright (c) 2010
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @version
1.1
* @version
2.0
**/
**/
class
MysqliDb
class
MysqliDb
{
{
...
@@ -74,6 +75,18 @@ class MysqliDb
...
@@ -74,6 +75,18 @@ class MysqliDb
* @var string
* @var string
*/
*/
protected
$_stmtError
;
protected
$_stmtError
;
/**
* Database credentials
*
* @var string
*/
protected
$host
;
protected
$username
;
protected
$password
;
protected
$db
;
protected
$port
;
/**
/**
* @param string $host
* @param string $host
* @param string $username
* @param string $username
...
@@ -83,17 +96,30 @@ class MysqliDb
...
@@ -83,17 +96,30 @@ class MysqliDb
*/
*/
public
function
__construct
(
$host
,
$username
,
$password
,
$db
,
$port
=
NULL
)
public
function
__construct
(
$host
,
$username
,
$password
,
$db
,
$port
=
NULL
)
{
{
$this
->
host
=
$host
;
$this
->
username
=
$username
;
$this
->
password
=
$password
;
$this
->
db
=
$db
;
if
(
$port
==
NULL
)
if
(
$port
==
NULL
)
$port
=
ini_get
(
'mysqli.default_port'
);
$this
->
port
=
ini_get
(
'mysqli.default_port'
);
else
$this
->
port
=
$port
;
$this
->
_mysqli
=
new
mysqli
(
$host
,
$username
,
$password
,
$db
,
$port
)
$this
->
connect
();
or
die
(
'There was a problem connecting to the database'
);
$this
->
_mysqli
->
set_charset
(
'utf8'
);
self
::
$_instance
=
$this
;
self
::
$_instance
=
$this
;
}
}
/**
* A method to connect to the database
*
*/
public
function
connect
()
{
$this
->
_mysqli
=
new
mysqli
(
$this
->
host
,
$this
->
username
,
$this
->
password
,
$this
->
db
,
$this
->
port
)
or
die
(
'There was a problem connecting to the database'
);
$this
->
_mysqli
->
set_charset
(
'utf8'
);
}
/**
/**
* A method of returning the static instance to allow access to the
* A method of returning the static instance to allow access to the
* instantiated object from within another class.
* instantiated object from within another class.
...
@@ -134,7 +160,8 @@ class MysqliDb
...
@@ -134,7 +160,8 @@ class MysqliDb
*/
*/
public
function
rawQuery
(
$query
,
$bindParams
=
null
)
public
function
rawQuery
(
$query
,
$bindParams
=
null
)
{
{
$this
->
_query
=
filter_var
(
$query
,
FILTER_SANITIZE_STRING
);
$this
->
_query
=
filter_var
(
$query
,
FILTER_SANITIZE_MAGIC_QUOTES
,
FILTER_FLAG_NO_ENCODE_QUOTES
);
$stmt
=
$this
->
_prepareQuery
();
$stmt
=
$this
->
_prepareQuery
();
if
(
is_array
(
$bindParams
)
===
true
)
{
if
(
is_array
(
$bindParams
)
===
true
)
{
...
@@ -280,7 +307,7 @@ class MysqliDb
...
@@ -280,7 +307,7 @@ class MysqliDb
*
*
* @return MysqliDb
* @return MysqliDb
*/
*/
public
function
where
(
$whereProp
,
$whereValue
)
public
function
where
(
$whereProp
,
$whereValue
=
null
)
{
{
$this
->
_where
[
$whereProp
]
=
Array
(
"AND"
,
$whereValue
);
$this
->
_where
[
$whereProp
]
=
Array
(
"AND"
,
$whereValue
);
return
$this
;
return
$this
;
...
@@ -296,7 +323,7 @@ class MysqliDb
...
@@ -296,7 +323,7 @@ class MysqliDb
*
*
* @return MysqliDb
* @return MysqliDb
*/
*/
public
function
orWhere
(
$whereProp
,
$whereValue
)
public
function
orWhere
(
$whereProp
,
$whereValue
=
null
)
{
{
$this
->
_where
[
$whereProp
]
=
Array
(
"OR"
,
$whereValue
);
$this
->
_where
[
$whereProp
]
=
Array
(
"OR"
,
$whereValue
);
return
$this
;
return
$this
;
...
@@ -339,7 +366,7 @@ class MysqliDb
...
@@ -339,7 +366,7 @@ class MysqliDb
{
{
$allowedDirection
=
Array
(
"ASC"
,
"DESC"
);
$allowedDirection
=
Array
(
"ASC"
,
"DESC"
);
$orderbyDirection
=
strtoupper
(
trim
(
$orderbyDirection
));
$orderbyDirection
=
strtoupper
(
trim
(
$orderbyDirection
));
$orderByField
=
filter_var
(
$orderByField
,
FILTER_SANITIZE_STRING
);
$orderByField
=
preg_replace
(
"/[^-a-z0-9\.\(\),_]+/i"
,
''
,
$orderByField
);
if
(
empty
(
$orderbyDirection
)
||
!
in_array
(
$orderbyDirection
,
$allowedDirection
))
if
(
empty
(
$orderbyDirection
)
||
!
in_array
(
$orderbyDirection
,
$allowedDirection
))
die
(
'Wrong order direction: '
.
$orderbyDirection
);
die
(
'Wrong order direction: '
.
$orderbyDirection
);
...
@@ -359,7 +386,7 @@ class MysqliDb
...
@@ -359,7 +386,7 @@ class MysqliDb
*/
*/
public
function
groupBy
(
$groupByField
)
public
function
groupBy
(
$groupByField
)
{
{
$groupByField
=
filter_var
(
$groupByField
,
FILTER_SANITIZE_STRING
);
$groupByField
=
preg_replace
(
"/[^-a-z0-9\.\(\),_]+/i"
,
''
,
$groupByField
);
$this
->
_groupBy
[]
=
$groupByField
;
$this
->
_groupBy
[]
=
$groupByField
;
return
$this
;
return
$this
;
...
@@ -387,6 +414,18 @@ class MysqliDb
...
@@ -387,6 +414,18 @@ class MysqliDb
return
$this
->
_mysqli
->
real_escape_string
(
$str
);
return
$this
->
_mysqli
->
real_escape_string
(
$str
);
}
}
/**
* Method to call mysqli->ping() to keep unused connections open on
* long-running scripts, or to reconnect timed out connections (if php.ini has
* global mysqli.reconnect set to true). Can't do this directly using object
* since _mysqli is protected.
*
* @return bool True if connection is up
*/
public
function
ping
()
{
return
$this
->
_mysqli
->
ping
();
}
/**
/**
* This method is needed for prepared statements. They require
* This method is needed for prepared statements. They require
* the data type of the field to be bound with "i" s", etc.
* the data type of the field to be bound with "i" s", etc.
...
@@ -405,6 +444,7 @@ class MysqliDb
...
@@ -405,6 +444,7 @@ class MysqliDb
return
's'
;
return
's'
;
break
;
break
;
case
'boolean'
:
case
'integer'
:
case
'integer'
:
return
'i'
;
return
'i'
;
break
;
break
;
...
@@ -420,6 +460,17 @@ class MysqliDb
...
@@ -420,6 +460,17 @@ class MysqliDb
return
''
;
return
''
;
}
}
/**
* Helper function to add variables into bind parameters array
*
* @param string Variable value
*/
protected
function
_bindParam
(
$value
)
{
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$value
);
array_push
(
$this
->
_bindParams
,
$value
);
}
/**
/**
* 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.
...
@@ -449,17 +500,16 @@ class MysqliDb
...
@@ -449,17 +500,16 @@ class MysqliDb
if
(
$isInsert
!==
false
)
{
if
(
$isInsert
!==
false
)
{
//is insert statement
//is insert statement
$this
->
_query
.=
'(
'
.
implode
(
array_keys
(
$tableData
),
', '
)
.
'
)'
;
$this
->
_query
.=
'(
`'
.
implode
(
array_keys
(
$tableData
),
'`, `'
)
.
'`
)'
;
$this
->
_query
.=
' VALUES('
;
$this
->
_query
.=
' VALUES('
;
}
}
foreach
(
$tableData
as
$column
=>
$value
)
{
foreach
(
$tableData
as
$column
=>
$value
)
{
if
(
$isUpdate
!==
false
)
if
(
$isUpdate
!==
false
)
$this
->
_query
.=
$column
.
"
= "
;
$this
->
_query
.=
"`"
.
$column
.
"`
= "
;
if
(
!
is_array
(
$value
))
{
if
(
!
is_array
(
$value
))
{
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$value
);
$this
->
_bindParam
(
$value
);
array_push
(
$this
->
_bindParams
,
$value
);
$this
->
_query
.=
'?, '
;
$this
->
_query
.=
'?, '
;
}
else
{
}
else
{
$key
=
key
(
$value
);
$key
=
key
(
$value
);
...
@@ -471,16 +521,16 @@ class MysqliDb
...
@@ -471,16 +521,16 @@ class MysqliDb
case
'[F]'
:
case
'[F]'
:
$this
->
_query
.=
$val
[
0
]
.
", "
;
$this
->
_query
.=
$val
[
0
]
.
", "
;
if
(
!
empty
(
$val
[
1
]))
{
if
(
!
empty
(
$val
[
1
]))
{
foreach
(
$val
[
1
]
as
$v
)
{
foreach
(
$val
[
1
]
as
$v
)
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$v
);
$this
->
_bindParam
(
$v
);
array_push
(
$this
->
_bindParams
,
$v
);
}
}
}
break
;
break
;
case
'[N]'
:
case
'[N]'
:
if
(
$val
==
null
)
$this
->
_query
.=
"!"
.
$column
.
", "
;
if
(
$val
==
null
)
else
$this
->
_query
.=
"!"
.
$val
.
", "
;
$this
->
_query
.=
"!"
.
$column
.
", "
;
break
;
else
$this
->
_query
.=
"!"
.
$val
.
", "
;
break
;
default
:
default
:
die
(
"Wrong operation"
);
die
(
"Wrong operation"
);
}
}
...
@@ -496,48 +546,49 @@ class MysqliDb
...
@@ -496,48 +546,49 @@ class MysqliDb
//Prepair the where portion of the query
//Prepair the where portion of the query
$this
->
_query
.=
' WHERE '
;
$this
->
_query
.=
' WHERE '
;
foreach
(
$this
->
_where
as
$column
=>
$value
)
{
foreach
(
$this
->
_where
as
$column
=>
$value
)
{
$andOr
=
''
;
//value[0] -- AND/OR, value[1] -- condition array
// if its not a first condition insert its concatenator (AND or OR)
// if its not a first condition insert its concatenator (AND or OR)
if
(
array_search
(
$column
,
array_keys
(
$this
->
_where
))
!=
0
)
if
(
array_search
(
$column
,
array_keys
(
$this
->
_where
))
!=
0
)
$
andOr
=
' '
.
$value
[
0
]
.
' '
;
$
this
->
_query
.
=
' '
.
$value
[
0
]
.
' '
;
$value
=
$value
[
1
];
if
(
is_array
(
$value
[
1
]))
{
$comparison
=
' = ? '
;
//value[0] -- AND/OR, value[1] -- condition array
if
(
is_array
(
$value
)
)
{
// if the value is an array, then this isn't a basic = comparison
// if the value is an array, then this isn't a basic = comparison
$key
=
key
(
$value
);
$key
=
key
(
$value
[
1
]
);
$val
=
$value
[
$key
];
$val
=
$value
[
1
][
$key
];
switch
(
strtolower
(
$key
)
)
{
switch
(
strtolower
(
$key
)
)
{
case
'0'
:
$comparison
=
''
;
foreach
(
$value
[
1
]
as
$v
)
$this
->
_bindParam
(
$v
);
break
;
case
'not in'
:
case
'in'
:
case
'in'
:
$comparison
=
'
IN
('
;
$comparison
=
'
'
.
$key
.
'
('
;
foreach
(
$val
as
$v
){
foreach
(
$val
as
$v
){
$comparison
.=
' ?,'
;
$comparison
.=
' ?,'
;
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$v
);
$this
->
_bindParam
(
$v
);
array_push
(
$this
->
_bindParams
,
$v
);
}
}
$comparison
=
rtrim
(
$comparison
,
','
)
.
' ) '
;
$comparison
=
rtrim
(
$comparison
,
','
)
.
' ) '
;
break
;
break
;
case
'not between'
:
case
'between'
:
case
'between'
:
$comparison
=
' BETWEEN ? AND ? '
;
$comparison
=
' '
.
$key
.
' ? AND ? '
;
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$val
[
0
]
);
$this
->
_bindParam
(
$val
[
0
]);
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$val
[
1
]
);
$this
->
_bindParam
(
$val
[
1
]);
array_push
(
$this
->
_bindParams
,
$val
[
0
]);
array_push
(
$this
->
_bindParams
,
$val
[
1
]);
break
;
break
;
default
:
default
:
// We are using a comparison operator with only one parameter after it
// We are using a comparison operator with only one parameter after it
$comparison
=
' '
.
$key
.
' ? '
;
$comparison
=
' '
.
$key
.
' ? '
;
// Determines what data type the where column is, for binding purposes.
$this
->
_bindParam
(
$val
);
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$val
);
array_push
(
$this
->
_bindParams
,
$val
);
}
}
}
else
if
(
$value
[
1
]
==
null
)
{
$comparison
=
''
;
}
else
{
}
else
{
// Determines what data type the where column is, for binding purposes.
$comparison
=
' = ? '
;
$this
->
_bindParams
[
0
]
.=
$this
->
_determineType
(
$value
);
$this
->
_bindParam
(
$value
[
1
]);
array_push
(
$this
->
_bindParams
,
$value
);
}
}
// Prepares the reset of the SQL query.
$this
->
_query
.=
$column
.
$comparison
;
$this
->
_query
.=
(
$andOr
.
$column
.
$comparison
);
}
}
}
}
...
@@ -572,10 +623,9 @@ class MysqliDb
...
@@ -572,10 +623,9 @@ class MysqliDb
// Prepare query
// Prepare query
$stmt
=
$this
->
_prepareQuery
();
$stmt
=
$this
->
_prepareQuery
();
// Bind parameters to stat
ment
// Bind parameters to stat
ement if any
if
(
$hasTableData
||
$hasConditional
)
{
if
(
count
(
$this
->
_bindParams
)
>
1
)
call_user_func_array
(
array
(
$stmt
,
'bind_param'
),
$this
->
refValues
(
$this
->
_bindParams
));
call_user_func_array
(
array
(
$stmt
,
'bind_param'
),
$this
->
refValues
(
$this
->
_bindParams
));
}
$this
->
_lastQuery
=
$this
->
replacePlaceHolders
(
$this
->
_query
,
$this
->
_bindParams
);
$this
->
_lastQuery
=
$this
->
replacePlaceHolders
(
$this
->
_query
,
$this
->
_bindParams
);
return
$stmt
;
return
$stmt
;
...
...
index.php
View file @
0a03d83b
<?php
<?php
require_once
(
'MysqliDb.php'
);
require_once
(
'MysqliDb.php'
);
error_reporting
(
E_ALL
);
$action
=
'adddb'
;
$data
=
array
();
$db
=
new
MysqliDb
(
'localhost'
,
'root'
,
'root'
,
'db'
);
function
printUsers
()
{
global
$db
;
$users
=
$db
->
get
(
"users"
);
if
(
$db
->
count
==
0
)
{
echo
"<td align=center colspan=4>No users found</td>"
;
return
;
}
foreach
(
$users
as
$u
)
{
echo
"<tr>
<td>
{
$u
[
'id'
]
}
</td>
<td>
{
$u
[
'login'
]
}
</td>
<td>
{
$u
[
'firstName'
]
}
{
$u
[
'lastName'
]
}
</td>
<td>
<a href='index.php?action=rm&id=
{
$u
[
'id'
]
}
'>rm</a> ::
<a href='index.php?action=mod&id=
{
$u
[
'id'
]
}
'>ed</a>
</td>
</tr>"
;
}
}
function
action_adddb
()
{
global
$db
;
$data
=
Array
(
'login'
=>
$_POST
[
'login'
],
'customerId'
=>
1
,
'firstName'
=>
$_POST
[
'firstName'
],
'lastName'
=>
$_POST
[
'lastName'
],
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
$_POST
[
'password'
]
.
'salt123'
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
)
);
$id
=
$db
->
insert
(
'users'
,
$data
);
header
(
"Location: index.php"
);
exit
;
}
function
action_moddb
()
{
global
$db
;
$data
=
Array
(
'login'
=>
$_POST
[
'login'
],
'customerId'
=>
1
,
'firstName'
=>
$_POST
[
'firstName'
],
'lastName'
=>
$_POST
[
'lastName'
],
);
$id
=
(
int
)
$_POST
[
'id'
];
$db
->
where
(
"customerId"
,
1
);
$db
->
where
(
"id"
,
$id
);
$db
->
update
(
'users'
,
$data
);
header
(
"Location: index.php"
);
exit
;
}
function
action_rm
()
{
global
$db
;
$id
=
(
int
)
$_GET
[
'id'
];
$db
->
where
(
"customerId"
,
1
);
$db
->
where
(
"id"
,
$id
);
$db
->
delete
(
'users'
);
header
(
"Location: index.php"
);
exit
;
}
function
action_mod
()
{
global
$db
;
global
$data
;
global
$action
;
$action
=
'moddb'
;
$id
=
(
int
)
$_GET
[
'id'
];
$db
->
where
(
"id"
,
$id
);
$data
=
$db
->
getOne
(
"users"
);
}
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'testdb'
);
if
(
$_GET
)
{
$f
=
"action_"
.
$_GET
[
'action'
];
if
(
function_exists
(
$f
))
{
$f
();
}
}
?>
?>
<!DOCTYPE html>
<!DOCTYPE html>
...
@@ -9,19 +94,32 @@ $db = new MysqliDb('localhost', 'root', 'root', 'db');
...
@@ -9,19 +94,32 @@ $db = new MysqliDb('localhost', 'root', 'root', 'db');
<html
lang=
"en"
>
<html
lang=
"en"
>
<head>
<head>
<meta
charset=
"utf-8"
>
<meta
charset=
"utf-8"
>
<title>
untitled
</title>
<title>
Users
</title>
</head>
</head>
<body>
<body>
<?php
$insertData
=
array
(
'title'
=>
'Inserted title'
,
'body'
=>
'Inserted body'
);
$results
=
$db
->
insert
(
'posts'
,
$insertData
);
<center>
print_r
(
$results
);
<h3>
Users:
</h3>
?>
<table
width=
'50%'
>
<tr
bgcolor=
'#cccccc'
>
<th>
ID
</th>
<th>
Login
</th>
<th>
Name
</th>
<th>
Action
</th>
</tr>
<?php
printUsers
();
?>
</table>
<hr
width=
50%
>
<form
action=
'index.php?action=
<?php
echo
$action
?>
'
method=
post
>
<input
type=
hidden
name=
'id'
value=
'
<?php
echo
$data
[
'id'
]
?>
'
>
<input
type=
text
name=
'login'
required
placeholder=
'Login'
value=
'
<?php
echo
$data
[
'login'
]
?>
'
>
<input
type=
text
name=
'firstName'
required
placeholder=
'First Name'
value=
'
<?php
echo
$data
[
'firstName'
]
?>
'
>
<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>
</table>
</center>
</body>
</body>
</html>
</html>
\ No newline at end of file
readme.md
View file @
0a03d83b
...
@@ -19,7 +19,7 @@ $data = Array ("login" => "admin",
...
@@ -19,7 +19,7 @@ $data = Array ("login" => "admin",
"firstName"
=>
"John"
,
"firstName"
=>
"John"
,
"lastName"
=>
'Doe'
"lastName"
=>
'Doe'
)
)
$id
=
$db
->
insert
(
'users'
,
$data
)
$id
=
$db
->
insert
(
'users'
,
$data
)
;
if
(
$id
)
if
(
$id
)
echo
'user was created. Id='
.
$id
;
echo
'user was created. Id='
.
$id
;
```
```
...
@@ -28,6 +28,7 @@ Insert with functions use
...
@@ -28,6 +28,7 @@ Insert with functions use
```
php
```
php
$data
=
Array
(
$data
=
Array
(
'login'
=>
'admin'
,
'login'
=>
'admin'
,
'active'
=>
true
,
'firstName'
=>
'John'
,
'firstName'
=>
'John'
,
'lastName'
=>
'Doe'
,
'lastName'
=>
'Doe'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
...
@@ -39,7 +40,7 @@ $data = Array(
...
@@ -39,7 +40,7 @@ $data = Array(
// Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
// Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
);
);
$id
=
$db
->
insert
(
'users'
,
$data
)
$id
=
$db
->
insert
(
'users'
,
$data
)
;
if
(
$id
)
if
(
$id
)
echo
'user was created. Id='
.
$id
;
echo
'user was created. Id='
.
$id
;
```
```
...
@@ -51,8 +52,8 @@ $data = Array (
...
@@ -51,8 +52,8 @@ $data = Array (
'lastName'
=>
'Tables'
,
'lastName'
=>
'Tables'
,
'editCount'
=>
$db
->
inc
(
2
),
'editCount'
=>
$db
->
inc
(
2
),
// editCount = editCount + 2;
// editCount = editCount + 2;
'
editBoolean
'
=>
$db
->
not
()
'
active
'
=>
$db
->
not
()
//
editBoolean = !editBoolean
;
//
active = !active
;
);
);
$db
->
where
(
'id'
,
1
);
$db
->
where
(
'id'
,
1
);
if
(
$db
->
update
(
'users'
,
$data
))
echo
'successfully updated'
;
if
(
$db
->
update
(
'users'
,
$data
))
echo
'successfully updated'
;
...
@@ -91,7 +92,7 @@ echo $user['id'];
...
@@ -91,7 +92,7 @@ echo $user['id'];
### Delete Query
### Delete Query
```
php
```
php
$db
->
where
(
'id'
,
1
);
$db
->
where
(
'id'
,
1
);
if
(
$db
->
delete
(
'
posts'
))
echo
'successfully deleted'
;
if
(
$db
->
delete
(
'
users'
))
echo
'successfully deleted'
;
```
```
### Generic Query Method
### Generic Query Method
...
@@ -126,7 +127,6 @@ $results = $db->get('users');
...
@@ -126,7 +127,6 @@ $results = $db->get('users');
// Gives: SELECT * FROM users WHERE id=1 AND login='admin';
// Gives: SELECT * FROM users WHERE id=1 AND login='admin';
```
```
Custom Operators:
```
php
```
php
$db
->
where
(
'id'
,
Array
(
'>='
=>
50
));
$db
->
where
(
'id'
,
Array
(
'>='
=>
50
));
$results
=
$db
->
get
(
'users'
);
$results
=
$db
->
get
(
'users'
);
...
@@ -136,6 +136,7 @@ $results = $db->get('users');
...
@@ -136,6 +136,7 @@ $results = $db->get('users');
BETWEEN:
BETWEEN:
```
php
```
php
$db
->
where
(
'id'
,
Array
(
'between'
=>
Array
(
4
,
20
)
)
);
$db
->
where
(
'id'
,
Array
(
'between'
=>
Array
(
4
,
20
)
)
);
//$db->where('id', Array('not between' => Array(4, 20) ) );
$results
=
$db
->
get
(
'users'
);
$results
=
$db
->
get
(
'users'
);
// Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20
// Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20
```
```
...
@@ -143,6 +144,7 @@ $results = $db->get('users');
...
@@ -143,6 +144,7 @@ $results = $db->get('users');
IN:
IN:
```
php
```
php
$db
->
where
(
'id'
,
Array
(
'in'
=>
Array
(
1
,
5
,
27
,
-
1
,
'd'
)
)
);
$db
->
where
(
'id'
,
Array
(
'in'
=>
Array
(
1
,
5
,
27
,
-
1
,
'd'
)
)
);
//$db->where('id', Array( 'not in' => Array(1, 5, 27, -1, 'd') ) );
$results
=
$db
->
get
(
'users'
);
$results
=
$db
->
get
(
'users'
);
// Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd');
// Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd');
```
```
...
@@ -162,12 +164,26 @@ $results = $db->get("users");
...
@@ -162,12 +164,26 @@ $results = $db->get("users");
// Gives: SELECT * FROM users where lastName <=> NULL
// Gives: SELECT * FROM users where lastName <=> NULL
```
```
Also you can use raw where conditions:
```
php
$db
->
where
(
"id != companyId"
);
$results
=
$db
->
get
(
"users"
);
```
Or raw condition with variables:
```
php
$db
->
where
(
"id = ? or id = ?"
,
Array
(
6
,
2
));
$res
=
$db
->
get
(
"users"
);
// Gives: SELECT * FROM users WERE id = 2 or id = 2;
```
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
```
php
```
php
$results
=
$db
$results
=
$db
->
where
(
'id'
,
1
)
->
where
(
'id'
,
1
)
->
where
(
'
title'
,
'MyTitle
'
)
->
where
(
'
login'
,
'admin
'
)
->
get
(
'users'
);
->
get
(
'users'
);
```
```
...
@@ -194,3 +210,15 @@ $db->where("u.id", 6);
...
@@ -194,3 +210,15 @@ $db->where("u.id", 6);
$products
=
$db
->
get
(
"products p"
,
null
,
"u.name, p.productName"
);
$products
=
$db
->
get
(
"products p"
,
null
,
"u.name, p.productName"
);
print_r
(
$products
);
print_r
(
$products
);
```
```
### Helper commands
Reconnect in case mysql connection died
```
php
if
(
!
$db
->
ping
())
$db
->
connect
()
```
Obtain an initialized instance of the class from another class
```
php
$db
=
MysqliDb
::
getInstance
();
```
tests.php
View file @
0a03d83b
...
@@ -8,6 +8,7 @@ if(!$db) die("Database error");
...
@@ -8,6 +8,7 @@ if(!$db) die("Database error");
$tables
=
Array
(
$tables
=
Array
(
'users'
=>
Array
(
'users'
=>
Array
(
'login'
=>
'char(10) not null'
,
'login'
=>
'char(10) not null'
,
'active'
=>
'bool default 0'
,
'customerId'
=>
'int(10) not null'
,
'customerId'
=>
'int(10) not null'
,
'firstName'
=>
'char(10) not null'
,
'firstName'
=>
'char(10) not null'
,
'lastName'
=>
'char(10)'
,
'lastName'
=>
'char(10)'
,
...
@@ -15,37 +16,67 @@ $tables = Array (
...
@@ -15,37 +16,67 @@ $tables = Array (
'createdAt'
=>
'datetime'
,
'createdAt'
=>
'datetime'
,
'expires'
=>
'datetime'
,
'expires'
=>
'datetime'
,
'loginCount'
=>
'int(10) default 0'
'loginCount'
=>
'int(10) default 0'
),
'products'
=>
Array
(
'customerId'
=>
'int(10) not null'
,
'userId'
=>
'int(10) not null'
,
'productName'
=>
'char(50)'
)
)
);
);
$data
=
Array
(
$data
=
Array
(
Array
(
'login'
=>
'user1'
,
'users'
=>
Array
(
'customerId'
=>
10
,
Array
(
'login'
=>
'user1'
,
'firstName'
=>
'John'
,
'customerId'
=>
10
,
'lastName'
=>
'Doe'
,
'firstName'
=>
'John'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
'lastName'
=>
'Doe'
,
'createdAt'
=>
$db
->
now
(),
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
'expires'
=>
$db
->
now
(
'+1Y'
),
'createdAt'
=>
$db
->
now
(),
'loginCount'
=>
$db
->
inc
()
'expires'
=>
$db
->
now
(
'+1Y'
),
),
'loginCount'
=>
$db
->
inc
()
Array
(
'login'
=>
'user2'
,
),
'customerId'
=>
10
,
Array
(
'login'
=>
'user2'
,
'firstName'
=>
'Mike'
,
'customerId'
=>
10
,
'lastName'
=>
NULL
,
'firstName'
=>
'Mike'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'lastName'
=>
NULL
,
'createdAt'
=>
$db
->
now
(),
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'expires'
=>
$db
->
now
(
'+1Y'
),
'createdAt'
=>
$db
->
now
(),
'loginCount'
=>
$db
->
inc
(
2
)
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
2
)
),
Array
(
'login'
=>
'user3'
,
'active'
=>
true
,
'customerId'
=>
11
,
'firstName'
=>
'Pete'
,
'lastName'
=>
'D'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
3
)
)
),
),
Array
(
'login'
=>
'user3'
,
'products'
=>
Array
(
'customerId'
=>
11
,
Array
(
'customerId'
=>
1
,
'firstName'
=>
'Pete'
,
'userId'
=>
1
,
'lastName'
=>
'D'
,
'productName'
=>
'product1'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
),
'createdAt'
=>
$db
->
now
(),
Array
(
'customerId'
=>
1
,
'expires'
=>
$db
->
now
(
'+1Y'
),
'userId'
=>
1
,
'loginCount'
=>
$db
->
inc
(
3
)
'productName'
=>
'product2'
,
)
),
Array
(
'customerId'
=>
1
,
'userId'
=>
1
,
'productName'
=>
'product3'
,
),
Array
(
'customerId'
=>
1
,
'userId'
=>
2
,
'productName'
=>
'product4'
,
),
Array
(
'customerId'
=>
1
,
'userId'
=>
2
,
'productName'
=>
'product5'
,
),
)
);
);
function
createTable
(
$name
,
$data
)
{
function
createTable
(
$name
,
$data
)
{
...
@@ -64,12 +95,15 @@ foreach ($tables as $name => $fields) {
...
@@ -64,12 +95,15 @@ foreach ($tables as $name => $fields) {
createTable
(
$name
,
$fields
);
createTable
(
$name
,
$fields
);
}
}
foreach
(
$data
as
$d
)
{
$id
=
$db
->
insert
(
"users"
,
$d
);
foreach
(
$data
as
$name
=>
$datas
)
{
if
(
$id
)
foreach
(
$datas
as
$d
)
{
$d
[
'id'
]
=
$id
;
$id
=
$db
->
insert
(
$name
,
$d
);
else
{
if
(
$id
)
echo
"failed to insert: "
.
$db
->
getLastQuery
()
.
"
\n
"
.
$db
->
getLastError
();
$d
[
'id'
]
=
$id
;
else
{
echo
"failed to insert: "
.
$db
->
getLastQuery
()
.
"
\n
"
.
$db
->
getLastError
();
}
}
}
}
}
...
@@ -79,6 +113,23 @@ if ($db->count != 3) {
...
@@ -79,6 +113,23 @@ if ($db->count != 3) {
echo
"Invalid total insert count"
;
echo
"Invalid total insert count"
;
exit
;
exit
;
}
}
$db
->
where
(
"active"
,
true
);
$users
=
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
1
)
{
echo
"Invalid total insert count with boolean"
;
exit
;
}
$db
->
where
(
"active"
,
false
);
$db
->
update
(
"users"
,
Array
(
"active"
=>
$db
->
not
()));
$db
->
where
(
"active"
,
true
);
$users
=
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
3
)
{
echo
"Invalid total insert count with boolean"
;
exit
;
}
// TODO
// TODO
//$db->where("createdAt", Array (">" => $db->interval("-1h")));
//$db->where("createdAt", Array (">" => $db->interval("-1h")));
//$users = $db->get("users");
//$users = $db->get("users");
...
@@ -147,12 +198,29 @@ if ($db->count != 1) {
...
@@ -147,12 +198,29 @@ if ($db->count != 1) {
exit
;
exit
;
}
}
$db
->
join
(
"users u"
,
"p.userId=u.id"
,
"LEFT"
);
$db
->
where
(
"u.login"
,
'user2'
);
$db
->
orderBy
(
"CONCAT(u.login, u.firstName)"
);
$products
=
$db
->
get
(
"products p"
,
null
,
"u.login, p.productName"
);
if
(
$db
->
count
!=
2
)
{
echo
"Invalid products count on join ()"
;
exit
;
}
$db
->
where
(
"id = ? or id = ?"
,
Array
(
1
,
2
));
$res
=
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
2
)
{
echo
"Invalid users count on select with multiple params"
;
exit
;
}
$db
->
delete
(
"users"
);
$db
->
delete
(
"users"
);
$db
->
get
(
"users"
);
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
0
)
{
if
(
$db
->
count
!=
0
)
{
echo
"Invalid users count after delete"
;
echo
"Invalid users count after delete"
;
exit
;
exit
;
}
}
$db
->
delete
(
"products"
);
echo
"All done"
;
echo
"All done"
;
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
...
...
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