_php Tutorial for PDO Common class library

Source: Internet
Author: User
Tags getmessage
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); /...

  • 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.