PHP PDO Encapsulation

Source: Internet
Author: User
<?php
Class DB {
private static $host = "localhost";
private static $dbname = "Bipp_new";
private static $username = "root";
private static $password = "123456";
function __construct () {
$numargs = Func_num_args ();
}

static function con () {
return new PDO (' mysql:host= '. Self:: $host. ') Dbname= '. Self:: $dbname, Self:: $username, Self:: $password,
Array (Pdo::mysql_attr_init_command => "SET NAMES UTF8"));
}


/**
/@parma $sql: SELECT name, colour, calories from fruit WHERE calories <:calories and name =:name
/@param $param _arr:array (": Calories" =>123, ": User" => "user")
**/
static function query ($sql, $param _arr) {
$stmt = Db::con ()->prepare ($sql, Array (pdo::attr_cursor => pdo::cursor_fwdonly));
$stmt->execute ($param _arr);
return $stmt->fetchall ();
}

/**
/@parma $sql: INSERT INTO ' page ' (PAGENAME) values (:p agename)
/@param $param _arr:array (":p agename" => "new test Name")
**/
static function exec ($sql, $param _arr) {
$stmt = Db::con ()->prepare ($sql, Array (pdo::attr_cursor => pdo::cursor_fwdonly));
return $stmt->execute ($param _arr);
}


}
?>

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.