PDO encapsulation-php Tutorial

Source: Internet
Author: User
Tags pconnect
PDO encapsulation problems. The network uploads a PDO Mysql package. I think it is a problem. I learned JAVA before and just got in touch with PHP. The code is as follows: & lt ;? Php ** & nbsp; * database PDO Operations & nbsp; * classMysqlPdo {publicstatic $ PDOStatementnull; *** database connection parameter configuration * @ var PDO encapsulation problem.
The network uploads a PDO Mysql package. I think it is a problem. I learned JAVA before and just got in touch with PHP. The code is as follows:
/**
* Database PDO operations
*/
Class MysqlPdo {
Public static $ PDOStatement = null;
/**
* Database connection parameter configuration
* @ Var array
* @ Access public
*/
Public static $ config = array ();
/**
* Whether to use permanent connection
* @ Var bool
* @ Access public
*/
Public static $ pconnect = false;
/**
* Error message
* @ Var string
* @ Access public
*/
Public static $ error = '';
/**
* In single-piece mode, the unique instance of the Pdo class is saved and the connection resource of the database is saved.
* @ Var object
* @ Access public
*/
Protected static $ link;
/**
* Whether the database has been connected
* @ Var bool
* @ Access public
*/
Public static $ connected = false;
/**
* Database version
* @ Var string
* @ Access public
*/
Public static $ dbVersion = null;
/**
* Current SQL statement
* @ Var string
* @ Access public
*/
Public static $ queryStr = '';
/**
* Last inserted record ID
* @ Var integer
* @ Access public
*/
Public static $ lastInsertId = null;
/**
* Number of affected records returned
* @ Var integer
* @ Access public
*/
Public static $ numRows = 0;
// Transaction instruction count
Public static $ transTimes = 0;
/**
* Constructor,
* @ Param $ dbconfig database connection information, array ('servername', 'username', 'password', 'defaultdb', 'DB _ port', 'DB _ type ')
*/
Public function _ construct ($ dbConfig = ''){
If (! Class_exists ('pdo '))
Throw_exception ("not supported: PDO ");

// Use the default data definition if no parameters are transmitted
If (! Is_array ($ dbConfig )){
$ DbConfig = array ('hostname' => DB_HOST, 'username' => DB_USER, 'password' => DB_PWD, 'database' => DB_NAME, 'hostport' => DB_PORT, 'dbms '=> DB_TYPE, 'dsn' => DB_TYPE. ": host = ". DB_HOST. "; dbname = ". DB_NAME );
}
If (empty ($ dbConfig ['hostname'])
Throw_exception ("no database configuration defined ");
Self: $ config = $ dbConfig; // pass the input configuration parameters to the Static variable.
If (empty (self: $ config ['params']) // ???
Self: $ config ['params'] = array ();
/************************************ Gorgeous separation line **************************************** ***/
If (! Isset (self: $ link )){
$ Configs = self: $ config; // Copy the configuration information.
If (self: $ pconnect ){
$ Configs ['params'] [constant ('pdo: ATTR_PERSISTENT ')] = true;
}
Try {
Self: $ link = new PDO ($ configs ['dsn '], $ configs ['username'], $ configs ['password'], $ configs ['params']);
} Catch (PDOException $ e ){
Throw_exception ($ e-> getMessage ());

// Exit ('connection failed: '. $ e-> getMessage ());
}
If (! Self: $ link ){
Throw_exception ('pdo CONNECT error ');
Return false;
}
Self: $ link-> exec ('set names'. DB_CHARSET );
Self: $ dbVersion = self: $ link-> getAttribute (constant ("PDO: ATTR_SERVER_INFO "));
// Mark the connection successful
Self: $ connected = true;
// Cancel database connection configuration
Unset ($ configs );
}
Return self: $ link;
}
/**
* Release query results
* @ Access function
*/
Static function free (){
Self: $ PDOStatement = null;
}
/*************************************** **************************************** **************************/
/* Database operations */
/*************************************** **************************************** **************************/
/**
* Obtain all query data
* @ Access function
* @ Return array
*/
Static function getAll ($ SQL = null ){
Self: query ($ SQL );
// Return the dataset
$ Result = self ::$ PDOStatement-> fetchAll (constant ('pdo: FETCH_ASSOC '));
Return $ result;
}
/**
* Obtain a query result.
* @ Access function
* @ Param string $ SQL SQL command
* @ Param integer $ seek pointer position
* @ Return array
*/
Static function getRow ($ SQL = null ){

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.