Chinaunix首页 | 论坛 | 博客
  • 博客访问: 81128
  • 博文数量: 13
  • 博客积分: 384
  • 博客等级: 一等列兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-20 09:16
文章分类

全部博文(13)

文章存档

2011年(13)

最近访客

分类: 系统运维

2011-06-08 17:50:26


  1. $dbObj =& new sdb("mysql:host=localhost;dbname=testdb", 'username', 'password');

  1. <?php

  2. /**
  3.  * PDO SINGLETON CLASS
  4.  *
  5.  * @author Tony Landis
  6.  * @link
  7.  * @license Use how you like it, just please don't remove or alter this PHPDoc
  8.  */
  9. class sdb
  10. {
  11.     /**
  12.      * The singleton instance
  13.      *
  14.      */
  15.     static private $PDOInstance;
  16.      
  17.       /**
  18.        * Creates a PDO instance representing a connection to a database and makes the instance available as a singleton
  19.        *
  20.        * @param string $dsn The full DSN, eg: mysql:host=localhost;dbname=testdb
  21.        * @param string $username The user name for the DSN string. This parameter is optional for some PDO drivers.
  22.        * @param string $password The password for the DSN string. This parameter is optional for some PDO drivers.
  23.        * @param array $driver_options A key=>value array of driver-specific connection options
  24.        *
  25.        * @return PDO
  26.        */
  27.     public function __construct($dsn, $username=false, $password=false, $driver_options=false)
  28.     {
  29.         if(!self::$PDOInstance) {
  30.      try {
  31.              self::$PDOInstance = new PDO($dsn, $username, $password, $driver_options);
  32.             } catch (PDOException $e) {
  33.              die("PDO CONNECTION ERROR: " . $e->getMessage() . "
    "
    );
  34.             }
  35.         }
  36.           return self::$PDOInstance;          
  37.     }
  38.     
  39.       /**
  40.        * Initiates a transaction
  41.        *
  42.        * @return bool
  43.        */
  44.     public function beginTransaction() {
  45.         return self::$PDOInstance->beginTransaction();
  46.     }
  47.         
  48.     /**
  49.      * Commits a transaction
  50.      *
  51.      * @return bool
  52.      */
  53.     public function commit() {
  54.         return self::$PDOInstance->commit();
  55.     }
  56.     
  57.     /**
  58.      * Fetch the SQLSTATE associated with the last operation on the database handle
  59.      *
  60.      * @return string
  61.      */
  62.     public function errorCode() {
  63.         return self::$PDOInstance->errorCode();
  64.     }
  65.     
  66.     /**
  67.      * Fetch extended error information associated with the last operation on the database handle
  68.      *
  69.      * @return array
  70.      */
  71.     public function errorInfo() {
  72.         return self::$PDOInstance->errorInfo();
  73.     }
  74.     
  75.     /**
  76.      * Execute an SQL statement and return the number of affected rows
  77.      *
  78.      * @param string $statement
  79.      */
  80.     public function exec($statement) {
  81.         return self::$PDOInstance->exec($statement);
  82.     }
  83.     
  84.     /**
  85.      * Retrieve a database connection attribute
  86.      *
  87.      * @param int $attribute
  88.      * @return mixed
  89.      */
  90.     public function getAttribute($attribute) {
  91.         return self::$PDOInstance->getAttribute($attribute);
  92.     }

  93.     /**
  94.      * Return an array of available PDO drivers
  95.      *
  96.      * @return array
  97.      */
  98.     public function getAvailableDrivers(){
  99.         return Self::$PDOInstance->getAvailableDrivers();
  100.     }
  101.     
  102.     /**
  103.      * Returns the ID of the last inserted row or sequence value
  104.      *
  105.      * @param string $name Name of the sequence object from which the ID should be returned.
  106.      * @return string
  107.      */
  108.     public function lastInsertId($name) {
  109.         return self::$PDOInstance->lastInsertId($name);
  110.     }
  111.         
  112.        /**
  113.      * Prepares a statement for execution and returns a statement object
  114.      *
  115.      * @param string $statement A valid SQL statement for the target database server
  116.      * @param array $driver_options Array of one or more key=>value pairs to set attribute values for the PDOStatement obj
  117. returned
  118.      * @return PDOStatement
  119.      */
  120.     public function prepare ($statement, $driver_options=false) {
  121.         if(!$driver_options) $driver_options=array();
  122.         return self::$PDOInstance->prepare($statement, $driver_options);
  123.     }
  124.     
  125.     /**
  126.      * Executes an SQL statement, returning a result set as a PDOStatement object
  127.      *
  128.      * @param string $statement
  129.      * @return PDOStatement
  130.      */
  131.     public function query($statement) {
  132.         return self::$PDOInstance->query($statement);
  133.     }
  134.     
  135.     /**
  136.      * Execute query and return all rows in assoc array
  137.      *
  138.      * @param string $statement
  139.      * @return array
  140.      */
  141.     public function queryFetchAllAssoc($statement) {
  142.         return self::$PDOInstance->query($statement)->fetchAll(PDO::FETCH_ASSOC);
  143.     }
  144.     
  145.     /**
  146.      * Execute query and return one row in assoc array
  147.      *
  148.      * @param string $statement
  149.      * @return array
  150.      */
  151.     public function queryFetchRowAssoc($statement) {
  152.         return self::$PDOInstance->query($statement)->fetch(PDO::FETCH_ASSOC);     
  153.     }
  154.     
  155.     /**
  156.      * Execute query and select one column only
  157.      *
  158.      * @param string $statement
  159.      * @return mixed
  160.      */
  161.     public function queryFetchColAssoc($statement) {
  162.         return self::$PDOInstance->query($statement)->fetchColumn();     
  163.     }
  164.     
  165.     /**
  166.      * Quotes a string for use in a query
  167.      *
  168.      * @param string $input
  169.      * @param int $parameter_type
  170.      * @return string
  171.      */
  172.     public function quote ($input, $parameter_type=0) {
  173.         return self::$PDOInstance->quote($input, $parameter_type);
  174.     }
  175.     
  176.     /**
  177.      * Rolls back a transaction
  178.      *
  179.      * @return bool
  180.      */
  181.     public function rollBack() {
  182.         return self::$PDOInstance->rollBack();
  183.     }
  184.     
  185.     /**
  186.      * Set an attribute
  187.      *
  188.      * @param int $attribute
  189.      * @param mixed $value
  190.      * @return bool
  191.      */
  192.     public function setAttribute($attribute, $value ) {
  193.         return self::$PDOInstance->setAttribute($attribute, $value);
  194.     }
  195. }
  196. ?>
阅读(1970) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~