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