Class dbconfig {
Private static $ dbms = "mysql ";
Private static $ host = '2017. 0.0.1 ';
Private static $ port = '123 ';
Private static $ username = '';
Private static $ password = '';
Private static $ dbname = '';
Private static $ charset = 'utf-8 ';
Private static $ dsn;
/**
*
* @ Return returns the pdo dsn Configuration
*/
Public static function getdsn (){
If (! Isset (self: $ dsn )){
Self: $ dsn = self: $ dbms. ': host ='. self: $ host. '; port = '.
Self: $ port. '; dbname ='. self: $ dbname;
If (strlen (self: $ charset)> 0 ){
Self: $ dsn = self: $ dsn. '; charset ='. self: $ charset;
}
}
Return self: $ dsn;
}
/**
* Set the mysql database server host
* @ Param $ IP address of the host
*/
Public static function sethost ($ host ){
If (isset ($ host) & strlen ($ host)> 0)
Self: $ host = trim ($ host );
}
/**
* Set the port of the mysql database server
* @ Param $ port
*/
Public static function setport ($ port ){
If (isset ($ port) & strlen ($ port)> 0)
Self: $ port = trim ($ port );
}
/**
* Set the login username of the mysql database server
* @ Param $ username
*/
Public static function setusername ($ username ){
If (isset ($ username) & strlen ($ username)> 0)
Self: $ username = $ username;
}
/**
* Set the logon password of the mysql database server
* @ Param $ password
*/
Public static function setpassword ($ password ){
If (isset ($ password) & strlen ($ password)> 0)
Self: $ password = $ password;
}
/**
* Set the database instance name of the mysql database server
* @ Param $ dbname database instance name
*/
Public static function setdbname ($ dbname ){
If (isset ($ dbname) & strlen ($ dbname)> 0)
Self: $ dbname = $ dbname;
}
/**
* Set database Encoding
* @ Param $ charset
*/
Public static function setcharset ($ charset ){
If (isset ($ charset) & strlen ($ charset)> 0)
Self: $ charset = $ charset;
}
}
/**
* One database operation tool class
*
* @ Author zhjiun@gmail.com
*/
Class dbtemplate {
/**
* Multiple rows of records are returned.
* @ Param $ SQL
* @ Param $ parameters
* @ Return record data
*/
Public function queryrows ($ SQL, $ parameters = null ){
Return $ this-> exequery ($ SQL, $ parameters );
}
/**
* Returns a single record.
* @ Param $ SQL
* @ Param $ parameters
* @ Return
*/
Public function queryrow ($ SQL, $ parameters = null ){
$ Rs = $ this-> exequery ($ SQL, $ parameters );
If (count ($ rs)> 0 ){
Return $ rs [0];
} Else {
Return null;
}
}
/**
* Query a single field. An integer is returned.
* @ Param $ SQL
* @ Param $ parameters
* @ Return
*/
Public function queryforint ($ SQL, $ parameters = null ){
$ Rs = $ this-> exequery ($ SQL, $ parameters );
If (count ($ rs)> 0 ){
Return intval ($ rs [0] [0]);
} Else {
Return null;
}
}
/**
* Query a single field and return a floating point number (float)
* @ Param $ SQL
* @ Param $ parameters
* @ Return
*/
Public function queryforfloat ($ SQL, $ parameters = null ){
$ Rs = $ this-> exequery ($ SQL, $ parameters );
If (count ($ rs)> 0 ){
Return floatval ($ rs [0] [0]);
} Else {
Return null;
}
}
/**
* Query a single field and return a floating point number (double)
* @ Param $ SQL
* @ Param $ parameters
* @ Return
*/
Public function queryfordouble ($ SQL, $ parameters = null ){
$ Rs = $ this-> exequery ($ SQL, $ parameters );
If (count ($ rs)> 0 ){
Return doubleval ($ rs [0] [0]);
} Else {
Return null;
}
}
/**
* Query a single field and return objects. The actual type is determined by the database.
* @ Param $ SQL
* @ Param $ parameters
* @ Return
*/
Public function queryforobject ($ SQL, $ parameters = null ){
$ Rs = $ this-> exequery ($ SQL, $ parameters );
If (count ($ rs)> 0 ){
Return $ rs [0] [0];
} Else {
Return null;
}
}
/**
* Execute an update statement. insert/upadate/delete
* @ Param $ SQL
* @ Param $ parameters
* @ Return: Number of affected rows
*/
Public function update ($ SQL, $ parameters = null ){
Return $ this-> exeupdate ($ SQL, $ parameters );
}
Private function getconnection (){
$ Conn = new pdo (dbconfig: getdsn (), dbconfig: getusername (), dbconfig: getpassword ());
$ Conn-> setattribute (pdo: attr_case, pdo: case_upper );
Return $ conn;
}
Private function exequery ($ SQL, $ parameters = null ){
$ Conn = $ this-> getconnection ();
$ Stmt = $ conn-> prepare ($ SQL );
$ Stmt-> execute ($ parameters );
$ Rs = $ stmt-> fetchall ();
$ Stmt = null;
$ Conn = null;
Return $ rs;
}
Private function exeupdate ($ SQL, $ parameters = null ){
$ Conn = $ this-> getconnection ();
$ Stmt = $ conn-> prepare ($ SQL );
$ Stmt-> execute ($ parameters );
$ Affectedrows = $ stmt-> rowcount ();
$ Stmt = null;
$ Conn = null;
Return $ affectedrows;
}
}