免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 962 | 回复: 0
打印 上一主题 下一主题

PHP5 PDO Singleton Class [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:44 |只看该作者 |倒序浏览
http://tonylandis.com/php/php5-pdo-singleton-class/
  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 http://www.tonylandis.com
  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() . "<br/>");
  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. ?>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP