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
00529ef3
Commit
00529ef3
authored
Feb 15, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rework constructor to allow charset modification
parent
6d29fa7e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
12 deletions
+57
-12
MysqliDb.php
MysqliDb.php
+27
-10
readme.md
readme.md
+19
-2
tests.php
tests.php
+11
-0
No files found.
MysqliDb.php
View file @
00529ef3
...
...
@@ -92,6 +92,7 @@ class MysqliDb
protected
$password
;
protected
$db
;
protected
$port
;
protected
$charset
;
/**
* Is Subquery object
...
...
@@ -106,24 +107,36 @@ class MysqliDb
* @param string $db
* @param int $port
*/
public
function
__construct
(
$host
=
NULL
,
$username
=
NULL
,
$password
=
NULL
,
$db
=
NULL
,
$port
=
NULL
)
public
function
__construct
(
$host
=
NULL
,
$username
=
NULL
,
$password
=
NULL
,
$db
=
NULL
,
$port
=
NULL
,
$charset
=
'utf8'
)
{
$this
->
host
=
$host
;
$isSubQuery
=
false
;
// if params were passed as array
if
(
is_array
(
$host
))
{
foreach
(
$host
as
$key
=>
$val
)
$$key
=
$val
;
}
// if host were set as mysqli socket
if
(
is_object
(
$host
))
$this
->
_mysqli
=
$host
;
else
$this
->
host
=
$host
;
$this
->
username
=
$username
;
$this
->
password
=
$password
;
$this
->
db
=
$db
;
if
(
$port
==
NULL
)
$this
->
port
=
ini_get
(
'mysqli.default_port'
);
else
$this
->
port
=
$port
;
$this
->
port
=
$port
;
$this
->
charset
=
$charset
;
if
(
$
username
==
null
&&
$db
==
null
)
{
if
(
$
isSubQuery
)
{
$this
->
isSubQuery
=
true
;
return
;
}
// for subqueries we do not need database connection and redefine root instance
$this
->
connect
();
if
(
!
is_object
(
$host
))
$this
->
connect
();
$this
->
setPrefix
();
self
::
$_instance
=
$this
;
}
...
...
@@ -137,10 +150,14 @@ class MysqliDb
if
(
$this
->
isSubQuery
)
return
;
if
(
empty
(
$this
->
host
))
die
(
'Mysql host is not set'
);
$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'
);
if
(
$this
->
charset
)
$this
->
_mysqli
->
set_charset
(
$this
->
charset
);
}
/**
* A method of returning the static instance to allow access to the
...
...
@@ -1044,7 +1061,7 @@ class MysqliDb
*/
public
static
function
subQuery
(
$subQueryAlias
=
""
)
{
return
new
MysqliDb
(
$subQueryAlias
);
return
new
MysqliDb
(
Array
(
'host'
=>
$subQueryAlias
,
'isSubQuery'
=>
true
)
);
}
/**
...
...
readme.md
View file @
00529ef3
...
...
@@ -25,12 +25,29 @@ To utilize this class, first import MysqliDb.php into your project, and require
require_once
(
'MysqliDb.php'
);
```
After that, create a new instance of the class.
Simple initialization with utf8 charset by default:
```
php
$db
=
new
MysqliDb
(
'host'
,
'username'
,
'password'
,
'databaseName'
);
```
Advanced initialization. If no charset should be set charset, set it to null
```
php
$db
=
new
Mysqlidb
(
Array
(
'host'
=>
'host'
,
'username'
=>
'username'
,
'password'
=>
'password'
,
'db'
=>
'databaseName'
,
'port'
=>
3306
,
'charset'
=>
'utf8'
));
```
port and charset params are optional.
Reuse already connected mysqli:
```
php
$mysqli
=
new
mysqli
(
'host'
,
'username'
,
'password'
,
'databaseName'
);
$db
=
new
Mysqlidb
(
$mysqli
);
```
Its also possible to set a table prefix:
```
php
$db
->
setPrefix
(
'my_'
);
...
...
tests.php
View file @
00529ef3
...
...
@@ -5,6 +5,17 @@ error_reporting(E_ALL);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'testdb'
);
if
(
!
$db
)
die
(
"Database error"
);
$db
=
new
Mysqlidb
(
Array
(
'host'
=>
'localhost'
,
'username'
=>
'root'
,
'password'
=>
''
,
'db'
=>
'testdb'
,
'charset'
=>
null
));
if
(
!
$db
)
die
(
"Database error"
);
$mysqli
=
new
mysqli
(
'localhost'
,
'root'
,
''
,
'testdb'
);
$db
=
new
Mysqlidb
(
$mysqli
);
$prefix
=
't_'
;
$db
->
setPrefix
(
$prefix
);
...
...
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