Common PDO class libraries

Source: Internet
Author: User
1. Db. class. php & lt ;? Php // connect to the database classDb {staticpublicfunctiongetDB () {try {$ pdonewPDO (DB_DSN, DB_USER, DB_PWD); $ pdo-& gt; setAttribute (PDO: ATTR_PERSISTE

1. Db. class. php

// Connect to the database
Class Db {
Static public function getDB (){
Try {
$ Pdo = new PDO (DB_DSN, DB_USER, DB_PWD );
$ Pdo-> setAttribute (PDO: ATTR_PERSISTENT, true); // Set the database connection to persistent connection.
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // sets the throw error
$ Pdo-> setAttribute (PDO: ATTR_ORACLE_NULLS, true); // you can change the string to NULL to the SQL NULL.
$ Pdo-> query ('set NAMES utf8'); // sets the database encoding.
} Catch (PDOException $ e ){
Exit ('database connection error, error message: '. $ e-> getMessage ());
}
Return $ pdo;
}
}
?>
// Connect to the database
Class Db {
Static public function getDB (){
Try {
$ Pdo = new PDO (DB_DSN, DB_USER, DB_PWD );
$ Pdo-> setAttribute (PDO: ATTR_PERSISTENT, true); // Set the database connection to persistent connection.
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); // sets the throw error
$ Pdo-> setAttribute (PDO: ATTR_ORACLE_NULLS, true); // you can change the string to NULL to the SQL NULL.
$ Pdo-> query ('set NAMES utf8'); // sets the database encoding.
} Catch (PDOException $ e ){
Exit ('database connection error, error message: '. $ e-> getMessage ());
}
Return $ pdo;
}
}
?>
2. Model. class. php


// Operate SQL
Class Model {
/**
* The SQL addition, deletion, and modification operations return the affected number of rows.
* @ Param string $ SQL
* @ Return int
*/
Public function aud ($ SQL ){
Try {
$ Pdo = Db: getDB ();
$ Row = $ pdo-> exec ($ SQL );
} Catch (PDOException $ e ){
Exit ($ e-> getMessage ());
}
Return $ row;
}

/**
* All data is returned, and the PDOStatement object is returned.
* @ Param string $ SQL
* @ Return PDOStatement
*/
Public function getAll ($ SQL ){
Try {
$ Pdo = Db: getDB ();
$ Result = $ pdo-> query ($ SQL );
Return $ result;
} Catch (PDOException $ e ){
Exit ($ e-> getMessage ());
}
}
}
?>


From Lee.'s column

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.