Self-written PDO database abstraction layer MySQL for example

Source: Internet
Author: User

Class pdo_database{
function __construct ($type _database, $host _name, $dbname, $user _role, $pass _role, $sql) {
$dbms = $type _database;
Database type Oracle with ODI, for developers, using different databases, just change this, don't remember so many functions
$host = $host _name; Database host Name
$dbName = $dbname; The database used
$user = $user _role; Database connection user Name
$pass = $pass _role; The corresponding password
$DSN = "$dbms: host= $host;d bname= $dbName";

try {
$DBH = new PDO ($DSN, $user, $pass); Initializes a PDO object, which is the creation of a database connection object $DBH
echo "Connection success <br/>";
$DBH = null;
} catch (Pdoexception $e) {
Die ("error!:". $e->getmessage (). "<br/>");
}
The default is not a long connection, if you need a long database connection, you need to add one last parameter: Array (pdo::attr_persistent = True) becomes this:
$db = new PDO ($DSN, $user, $pass, array (pdo::attr_persistent = true));
$db->setattribute (Pdo::attr_case, Pdo::case_upper); Setting properties
$db->query ("Set names UTF8");
$rs = $db->query ($sql);
$rs->setfetchmode (PDO::FETCH_ASSOC);
$result _arr = $rs->fetchall ();
Dump ($result _arr);
}
}
$pdo _mysql = new Pdo_database ("MySQL", "localhost", "Shunong", "Root", "", "SELECT * from Sn_admin");

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.