1, Db.class.php
Connecting to a database
Class Db {
static public Function Getdb () {
try {
$pdo = new PDO (DB_DSN, Db_user, db_pwd);
$pdo->setattribute (Pdo::attr_persistent, true); To set a database connection as a persistent connection
$pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception); Set throw error
$pdo->setattribute (Pdo::attr_oracle_nulls, true); Set NULL to convert the string to SQL when null
$pdo->query (' SET NAMES utf8 '); Set Database encoding
} catch (Pdoexception $e) {
Exit (' Database connection error, error message: '. $e->getmessage ());
}
return $pdo;
}
}
?>
Connecting to a database
Class Db {
static public Function Getdb () {
try {
$pdo = new PDO (DB_DSN, Db_user, db_pwd);
$pdo->setattribute (Pdo::attr_persistent, true); To set a database connection as a persistent connection
$pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception); Set throw error
$pdo->setattribute (Pdo::attr_oracle_nulls, true); Set NULL to convert the string to SQL when null
$pdo->query (' SET NAMES utf8 '); Set Database encoding
} catch (Pdoexception $e) {
Exit (' Database connection error, error message: '. $e->getmessage ());
}
return $pdo;
}
}
?>
2, Model.class.php
Manipulating SQL
Class Model {
/**
* SQL additions and deletions, returns the number of rows affected
* @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;
}
/**
* Return all data, return Pdostatement object
* @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 ());
}
}
}
?>
Excerpt from Lee's column
http://www.bkjia.com/PHPjc/478464.html www.bkjia.com true http://www.bkjia.com/PHPjc/478464.html techarticle 1, Db.class.php? PHP//Connection database class Db {static public function Getdb () {try {$pdo = new PDO (DB_DSN, Db_user, Db_pwd) ; $pdo-setattribute (Pdo::attr_persistent, true); /...