note 97682 added to class.pdo

From: Date: Sun, 02 May 2010 22:47:51 +0000
Subject: note 97682 added to class.pdo
Groups: php.notes 
Request: Send a blank email to php-notes+get-168875@lists.php.net to get a copy of this message
Here is an singleton PDO example: ###### config.ini ###### db_driver=mysql db_user=root db_password=924892xp [dsn] host=localhost port=3306 dbname=localhost [db_options] PDO::MYSQL_ATTR_INIT_COMMAND=set names utf8 [db_attributes] ATTR_ERRMODE=ERRMODE_EXCEPTION ############ <?php class Database { private static $link = null ; private static function getLink ( ) { if ( self :: $link ) { return self :: $link ; } $ini = _BASE_DIR . "config.ini" ; $parse = parse_ini_file ( $ini , true ) ; $driver = $parse [ "db_driver" ] ; $dsn = "${driver}:" ; $user = $parse [ "db_user" ] ; $password = $parse [ "db_password" ] ; $options = $parse [ "db_options" ] ; $attributes = $parse [ "db_attributes" ] ; foreach ( $parse [ "dsn" ] as $k => $v ) { $dsn .= "${k}=${v};" ; } self :: $link = new PDO ( $dsn, $user, $password, $options ) ; foreach ( $attributes as $k => $v ) { self :: $link -> setAttribute ( constant ( "PDO::{$k}" ) , constant ( "PDO::{$v}" ) ) ; } return self :: $link ; } public static function __callStatic ( $name, $args ) { $callback = array ( self :: getLink ( ), $name ) ; return call_user_func_array ( $callback , $args ) ; } } ?> <?php // examples $stmt = Database :: prepare ( "SELECT 'something' ;" ) ; $stmt -> execute ( ) ; var_dump ( $stmt -> fetchAll ( ) ) ; $stmt -> closeCursor ( ) ; ?> ---- Server IP: 203.169.204.174 Probable Submitter: 183.178.51.229 ---- Manual Page -- http://www.php.net/manual/en/class.pdo.php Edit -- https://master.php.net/note/edit/97682 Del: integrated -- https://master.php.net/note/delete/97682/integrated Del: useless -- https://master.php.net/note/delete/97682/useless Del: bad code -- https://master.php.net/note/delete/97682/bad+code Del: spam -- https://master.php.net/note/delete/97682/spam Del: non-english -- https://master.php.net/note/delete/97682/non-english Del: in docs -- https://master.php.net/note/delete/97682/in+docs Del: other reasons-- https://master.php.net/note/delete/97682 Reject -- https://master.php.net/note/reject/97682 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168875) next »