Mysql database operations implemented by PHP PDO

Source: Internet
Author: User
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 is a collection of database access operations, which is very useful and requires friends.

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 is a collection of database access operations, which is very useful and requires friends.

This article describes the mysql database operation class for implementing PDO in PHP. Share it with you for your reference. The specific analysis is as follows:

Dbconfig is used to configure database access information, including the server address, port, database instance name, user name, user password, and character set.

The dbtemplate class integrates database access operations, mainly including 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.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.