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
5eba6215
Commit
5eba6215
authored
Oct 23, 2010
by
Josh Campbell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Features
parent
8f36a33f
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
192 additions
and
107 deletions
+192
-107
.gitignore
.gitignore
+5
-0
MysqlDb.php
MysqlDb.php
+149
-83
changelog.md
changelog.md
+12
-0
index.php
index.php
+12
-11
readme.md
readme.md
+14
-13
No files found.
.gitignore
0 → 100644
View file @
5eba6215
#Mac OS X files
.DS_Store
#Vim leave-behinds
*.swp
\ No newline at end of file
MysqlDb.php
View file @
5eba6215
<?php
class
MysqlDB
{
protected
$_mysql
;
protected
$_where
=
array
();
/**
* MySqliDb Class
*
* @category Database Access
* @package MysqliDB
* @author Jeffery Way <jeffrey@jeffrey-way.com>
* @author Josh Campbell <josh.campbell@teslacity.com>
* @copyright Copyright (c) 2010 Jeffery Way
* @license http://opensource.org/licenses/gpl-3.0.html GNU Public License
* @version 1.1
**/
class
MysqliDB
{
protected
static
$_instance
;
protected
$_mysqli
;
protected
$_query
;
protected
$_where
=
array
();
protected
$_whereTypeList
;
protected
$_paramTypeList
;
protected
$_crudType
=
null
;
public
function
__construct
(
$host
,
$username
,
$password
,
$db
)
{
$this
->
_mysql
=
new
mysqli
(
$host
,
$username
,
$password
,
$db
)
or
die
(
'There was a problem connecting to the database'
);
$this
->
_mysqli
=
new
mysqli
(
$host
,
$username
,
$password
,
$db
)
or
die
(
'There was a problem connecting to the database'
);
self
::
$_instance
=
$this
;
}
/**
* A method of returning the static instance to allow access to the
* instantiated object from within another class.
* Inheriting this class would require reloading connection info.
*
* @return object Returns the current instance.
*/
public
static
function
getInstance
()
{
return
self
::
$_instance
;
}
/**
* A method of returning the static instance.
*
* @return object Returns the current instance.
*/
protected
function
reset
()
{
$this
->
_where
=
array
();
unset
(
$this
->
_query
);
unset
(
$this
->
_whereTypeList
);
unset
(
$this
->
_paramTypeList
);
}
/**
...
...
@@ -18,12 +56,13 @@ class MysqlDB {
* @param int $numRows The number of rows total to return.
* @return array Contains the returned rows from the query.
*/
public
function
query
(
$query
)
public
function
query
(
$query
,
$numRows
=
NULL
)
{
$this
->
_query
=
filter_var
(
$query
,
FILTER_SANITIZE_STRING
);
$stmt
=
$this
->
_prepareQuery
();
$stmt
=
$this
->
_buildQuery
(
$numRows
);
$stmt
->
execute
();
$this
->
reset
();
$results
=
$this
->
_dynamicBindResults
(
$stmt
);
return
$results
;
}
...
...
@@ -37,13 +76,13 @@ class MysqlDB {
*/
public
function
get
(
$tableName
,
$numRows
=
NULL
)
{
$this
->
_crudType
=
'read'
;
$this
->
_query
=
"SELECT * FROM
$tableName
"
;
$stmt
=
$this
->
_buildQuery
(
$numRows
);
$stmt
->
execute
();
$this
->
reset
();
$results
=
$this
->
_dynamicBindResults
(
$stmt
);
return
$results
;
}
...
...
@@ -55,13 +94,13 @@ class MysqlDB {
*/
public
function
insert
(
$tableName
,
$insertData
)
{
$this
->
_crudType
=
'insert'
;
$this
->
_query
=
"INSERT into
$tableName
"
;
$stmt
=
$this
->
_buildQuery
(
NULL
,
$insertData
);
$stmt
->
execute
();
$this
->
reset
();
if
(
$stmt
->
affected_rows
)
return
true
;
(
$stmt
->
affected_rows
)
?
$result
=
$stmt
->
insert_id
:
$result
=
false
;
return
$result
;
}
/**
...
...
@@ -73,10 +112,12 @@ class MysqlDB {
*/
public
function
update
(
$tableName
,
$tableData
)
{
$this
->
_crudType
=
'update'
;
$this
->
_query
=
"UPDATE
$tableName
SET "
;
$stmt
=
$this
->
_buildQuery
(
NULL
,
$tableData
);
$stmt
->
execute
();
$this
->
reset
();
if
(
$stmt
->
affected_rows
)
return
true
;
}
...
...
@@ -87,23 +128,22 @@ class MysqlDB {
* @param string $tableName The name of the database table to work with.
* @return boolean Indicates success. 0 or 1.
*/
public
function
delete
(
$tableName
)
{
$this
->
_crudType
=
'delete'
;
public
function
delete
(
$tableName
)
{
$this
->
_query
=
"DELETE FROM
$tableName
"
;
$stmt
=
$this
->
_buildQuery
();
$stmt
->
execute
();
$this
->
reset
();
if
(
$stmt
->
affected_rows
)
return
true
;
}
/**
* This method allows you to specify
a WHERE statement
for SQL queries.
* This method allows you to specify
multipl WHERE statements
for SQL queries.
*
* @param string $whereProp
A string for the name of the database field to update
* @param mixed $whereValue The value
for th
e field.
* @param string $whereProp
The name of the database field.
* @param mixed $whereValue The value
of the databas
e field.
*/
public
function
where
(
$whereProp
,
$whereValue
)
{
...
...
@@ -149,46 +189,63 @@ class MysqlDB {
* @param array $tableData Should contain an array of data for updating the database.
* @return object Returns the $stmt object.
*/
protected
function
_buildQuery
(
$numRows
=
NULL
,
$tableData
=
false
)
protected
function
_buildQuery
(
$numRows
=
NULL
,
$tableData
=
NULL
)
{
$hasTableData
=
null
;
if
(
gettype
(
$tableData
)
===
'array'
)
{
$hasTableData
=
false
;
if
(
gettype
(
$tableData
)
===
'array'
)
$hasTableData
=
true
;
}
// Did the user call the "where" method?
if
(
!
empty
(
$this
->
_where
))
{
$keys
=
array_keys
(
$this
->
_where
);
$where_prop
=
$keys
[
0
];
$where_value
=
$this
->
_where
[
$where_prop
];
if
(
!
empty
(
$this
->
_where
))
{
// if update data was passed, filter through
// and create the SQL query, accordingly.
if
(
$hasTableData
)
{
// if update data was passed, filter through and create the SQL query, accordingly.
if
(
$hasTableData
)
{
$i
=
1
;
if
(
$this
->
_crudType
==
'update'
)
{
foreach
(
$tableData
as
$prop
=>
$value
)
{
$pos
=
strpos
(
$this
->
_query
,
'UPDATE'
);
if
(
$pos
!==
false
)
{
foreach
(
$tableData
as
$prop
=>
$value
)
{
// determines what data type the item is, for binding purposes.
$this
->
_paramTypeList
.=
$this
->
_determineType
(
$value
);
// prepares the reset of the SQL query.
if
(
$i
===
count
(
$tableData
))
{
$this
->
_query
.=
$prop
.
" = ? WHERE
$where_prop
= '
$where_value
'"
;
}
else
{
(
$i
===
count
(
$tableData
))
?
$this
->
_query
.=
$prop
.
' = ?'
:
$this
->
_query
.=
$prop
.
' = ?, '
;
}
$i
++
;
}
}
}
else
{
// no table data was passed. Might be SELECT statement.
$this
->
_paramTypeList
=
$this
->
_determineType
(
$where_value
);
$this
->
_query
.=
" WHERE "
.
$where_prop
.
"= ?"
;
}
//Prepair the where portion of the query
$this
->
_query
.=
' WHERE '
;
$i
=
1
;
foreach
(
$this
->
_where
as
$column
=>
$value
)
{
// Determines what data type the where column is, for binding purposes.
$this
->
_whereTypeList
.=
$this
->
_determineType
(
$value
);
// Prepares the reset of the SQL query.
(
$i
===
count
(
$this
->
_where
))
?
$this
->
_query
.=
$column
.
' = ?'
:
$this
->
_query
.=
$column
.
' = ? AND '
;
$i
++
;
}
}
// Determine if is INSERT query
if
(
$hasTableData
&&
$this
->
_crudType
==
'insert'
)
{
if
(
$hasTableData
)
{
$pos
=
strpos
(
$this
->
_query
,
'INSERT'
);
if
(
$pos
!==
false
)
{
//is insert statement
$keys
=
array_keys
(
$tableData
);
$values
=
array_values
(
$tableData
);
$num
=
count
(
$keys
);
...
...
@@ -206,11 +263,13 @@ class MysqlDB {
$num
--
;
}
}
}
// Did the user set a limit
if
(
isset
(
$numRows
))
{
$this
->
_query
.=
" LIMIT "
.
(
int
)
$numRows
;
}
// Prepare query
$stmt
=
$this
->
_prepareQuery
();
...
...
@@ -221,13 +280,21 @@ class MysqlDB {
foreach
(
$tableData
as
$prop
=>
$val
)
{
$args
[]
=
&
$tableData
[
$prop
];
}
call_user_func_array
(
array
(
$stmt
,
'bind_param'
),
$args
);
}
else
{
if
(
$this
->
_where
)
$stmt
->
bind_param
(
$this
->
_paramTypeList
,
$where_value
);
{
$wheres
=
array
();
$wheres
[]
=
$this
->
_whereTypeList
;
foreach
(
$this
->
_where
as
$prop
=>
$val
)
{
$wheres
[]
=
&
$this
->
_where
[
$prop
];
}
// Clear where method to prevent clashes with future operations;
$this
->
_where
=
array
();
call_user_func_array
(
array
(
$stmt
,
'bind_param'
),
$wheres
);
}
}
return
$stmt
;
}
...
...
@@ -268,9 +335,8 @@ class MysqlDB {
*/
protected
function
_prepareQuery
()
{
echo
$this
->
_query
;
if
(
!
$stmt
=
$this
->
_mysql
->
prepare
(
$this
->
_query
))
{
trigger_error
(
"Problem preparing query"
,
E_USER_ERROR
);
if
(
!
$stmt
=
$this
->
_mysqli
->
prepare
(
$this
->
_query
))
{
trigger_error
(
"Problem preparing query (
$this->_query
) "
.
$this
->
_mysqli
->
error
,
E_USER_ERROR
);
}
return
$stmt
;
}
...
...
@@ -278,7 +344,7 @@ class MysqlDB {
public
function
__destruct
()
{
$this
->
_mysql
->
close
();
$this
->
_mysql
i
->
close
();
}
}
}
// END class
\ No newline at end of file
changelog.md
0 → 100644
View file @
5eba6215
<h3>
Change Log
</h3>
<p>
Branch off master
</p>
<ul>
<li>
Changed package name to MySqliDb. This is a MySQLi wrapper.
</li>
<li>
Added support for multiple dynamicly bound where conditions.
</li>
<li>
Added state resetting after each query exicution to allow multiple calls to the same connection instance.
</li>
<li>
Added the ability to staticly retreive the current instance.
</li>
<li>
Added numRows support to the query method.
</li>
<li>
The triggered error in _prepareQuery() now also displays the SQL statment and the mysqli error message.
</li>
</ul>
\ No newline at end of file
index.php
View file @
5eba6215
<?php
require_once
(
'MysqlDb.php'
);
require_once
(
'Mysql
i
Db.php'
);
$Db
=
new
MysqlDb
(
'localhost'
,
'root'
,
'root'
,
'db'
);
$insertData
=
array
(
'title'
=>
'Inserted title'
,
'body'
=>
'Inserted body'
);
$results
=
$Db
->
insert
(
'posts'
,
$insertData
);
print_r
(
$results
);
$db
=
new
MysqliDb
(
'localhost'
,
'root'
,
'root'
,
'db'
);
?>
<!DOCTYPE html>
...
...
@@ -22,5 +13,15 @@ print_r($results);
</head>
<body>
<?php
$insertData
=
array
(
'title'
=>
'Inserted title'
,
'body'
=>
'Inserted body'
);
$results
=
$db
->
insert
(
'posts'
,
$insertData
);
print_r
(
$results
);
?>
</body>
</html>
\ No newline at end of file
readme.md
View file @
5eba6215
To utilize this class, first import Mysql
Db
.php into your project, and require it.
To utilize this class, first import Mysql
dbi
.php into your project, and require it.
<pre>
<code>
require_once('Mysql
D
b.php');
require_once('Mysql
id
b.php');
</code>
</pre>
...
...
@@ -10,7 +10,7 @@ After that, create a new instance of the class.
<pre>
<code>
$
Db = new MysqlD
b('host', 'username', 'password', 'databaseName');
$
db = new Mysqlid
b('host', 'username', 'password', 'databaseName');
</code>
</pre>
...
...
@@ -25,7 +25,7 @@ $insertData = array(
'body' => 'Inserted body'
);
if ( $
D
b->insert('posts', $insertData) ) echo 'success!';
if ( $
d
b->insert('posts', $insertData) ) echo 'success!';
</code>
</pre>
...
...
@@ -34,7 +34,7 @@ if ( $Db->insert('posts', $insertData) ) echo 'success!';
<pre>
<code>
$results = $
D
b->get('tableName', 'numberOfRows-optional');
$results = $
d
b->get('tableName', 'numberOfRows-optional');
print_r($results); // contains array of returned rows
</code>
</pre>
...
...
@@ -47,8 +47,8 @@ $updateData = array(
'fieldOne' => 'fieldValue',
'fieldTwo' => 'fieldValue'
);
$
D
b->where('id', int);
$results = $
D
b->update('tableName', $updateData);
$
d
b->where('id', int);
$results = $
d
b->update('tableName', $updateData);
</code>
</pre>
...
...
@@ -56,8 +56,8 @@ $results = $Db->update('tableName', $updateData);
<pre>
<code>
$
Db->where('id', integer
);
if ( $
D
b->delete('posts') ) echo 'successfully deleted';
$
db->where('id', int
);
if ( $
d
b->delete('posts') ) echo 'successfully deleted';
</code>
</pre>
...
...
@@ -65,17 +65,18 @@ if ( $Db->delete('posts') ) echo 'successfully deleted';
<pre>
<code>
$results = $
D
b->query('SELECT
*
from posts');
$results = $
d
b->query('SELECT
*
from posts');
print_r($results); // contains array of returned rows
</code>
</pre>
<h3>
Where Method
</h3>
<p>
This method allows you to specify the parameters of the query.
For now, it only accepts one key => value pair.
</p>
<p>
This method allows you to specify the parameters of the query.
</p>
<pre>
<code>
$Db->where('id', int);
$results = $Db->get('tableName');
$db->where('id', int);
$db->where('title', string);
$results = $db->get('tableName');
print_r($results); // contains array of returned rows
</code>
</pre>
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