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
4188a491
Commit
4188a491
authored
May 11, 2014
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ping() with reconnect abbility
parent
9b658126
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
6 deletions
+55
-6
MysqliDb.php
MysqliDb.php
+43
-6
readme.md
readme.md
+12
-0
No files found.
MysqliDb.php
View file @
4188a491
...
...
@@ -75,6 +75,18 @@ class MysqliDb
* @var string
*/
protected
$_stmtError
;
/**
* Database credentials
*
* @var string
*/
protected
$host
;
protected
$username
;
protected
$password
;
protected
$db
;
protected
$port
;
/**
* @param string $host
* @param string $username
...
...
@@ -84,17 +96,30 @@ class MysqliDb
*/
public
function
__construct
(
$host
,
$username
,
$password
,
$db
,
$port
=
NULL
)
{
$this
->
host
=
$host
;
$this
->
username
=
$username
;
$this
->
password
=
$password
;
$this
->
db
=
$db
;
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
)
or
die
(
'There was a problem connecting to the database'
);
$this
->
_mysqli
->
set_charset
(
'utf8'
);
$this
->
connect
();
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
* instantiated object from within another class.
...
...
@@ -388,6 +413,18 @@ class MysqliDb
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
* the data type of the field to be bound with "i" s", etc.
...
...
readme.md
View file @
4188a491
...
...
@@ -200,3 +200,15 @@ $db->where("u.id", 6);
$products
=
$db
->
get
(
"products p"
,
null
,
"u.name, p.productName"
);
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
();
```
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