note 96362 added to pdo.construct

From: Date: Tue, 23 Feb 2010 08:54:32 +0000
Subject: note 96362 added to pdo.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-166279@lists.php.net to get a copy of this message
<?php public function __construct($creds) { $type = (isset($creds['type'])) ? $creds['type'] :'mysql'; $username = (isset($creds['user'])) ? $creds['user'] : null ; $password = (isset($creds['pass'])) ? $creds['pass'] : null ; $options = (isset($creds['opts'])) ? $creds['opts'] : array() ; $dsn = $this->createDsn($type, $creds); parent::__construct($dsn, $username, $password, $options); } public function createDsn($type, $creds) { $dsn = $type . ':'; $struct = array( 'host' => null, 'port' => null, 'dbname' => null, 'unix_socket' => null ); $dsnarray = array_intersect_key($creds, $struct); foreach($dsnarray as $key => $value) { $dsn .= $key . '=' . $value . ';'; } $dsn = trim($dsn, ';'); return $dsn; } ?> of course you make $struct a muli-dimensional array and base keys on appropriate $type and add a check for 'unix_socket' and 'host' to make sure they are no both filled in ---- Server IP: 64.71.164.2 Probable Submitter: 24.38.172.22 ---- Manual Page -- http://www.php.net/manual/en/pdo.construct.php Edit -- https://master.php.net/note/edit/96362 Del: integrated -- https://master.php.net/note/delete/96362/integrated Del: useless -- https://master.php.net/note/delete/96362/useless Del: bad code -- https://master.php.net/note/delete/96362/bad+code Del: spam -- https://master.php.net/note/delete/96362/spam Del: non-english -- https://master.php.net/note/delete/96362/non-english Del: in docs -- https://master.php.net/note/delete/96362/in+docs Del: other reasons-- https://master.php.net/note/delete/96362 Reject -- https://master.php.net/note/reject/96362 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166279) next »