This article mainly introduces the mysql database operation class for implementing PDO in PHP. the dbconfig class is responsible for configuring database access information, and the dbtemplate class integrates database access operations, which is of great practical value, if you need it, refer to the following example to describe the mysql database operation class for implementing PDO in PHP. Share it with you for your reference. The specific analysis is as follows:
Dbconfig class is responsible for configuring database access informationIncluding: server address, port, database instance name, user name, user password, character set, etc.
The dbtemplate class integrates database access operations., Mainly includes the following operations:
1. queryrows: returns multiple rows of records.
2. queryrow: returns a single record.
3. queryforint: query a single field. an integer is returned.
4. queryforfloat: queries a single field and returns a floating point number (float)
5. queryfordouble: query a single field and return a floating point number (double)
6. queryforobject: query a single field and return an object. the actual type is determined by the database.
7. update: execute an update statement. insert/upadate/delete
The code is as follows:
The code is as follows:
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;
}
}
Pdo started with php5 and will use pdo by default in php6. Unlike the chaotic database operation methods in earlier versions, pdo unifies the database access methods and brings great convenience to programming, this tool class is based on pdo and simulates the jdbctemplate operation class in the java World spring Framework.
I hope this article will help you with PHP programming.