php - Multiple database connections using multiton or any design pattern -
i have following code multiple database connections. it's not design. static. don't know how improve it. can add more functions prepare query, presently want good/clean design. tried make multiton design pattern. requirements like, first connect 1 database, database details of other mysql clients, loop , connect each database , something. so, need multiple connections.
<?php class db_class{ private static $instance = array(); private function __construct(){ } public static function get_instance($type , $db_detail_array=array()){ $host = $db_detail_array['host']; $username = $db_detail_array['username']; $database = $db_detail_array['database']; $password = $db_detail_array['password']; if(empty($host) or empty($username) or empty($database) or empty($password)){ return; } if(empty(self::$instance[$type])) { self::$instance[$type] = new mysqli($host, $username, $password, $database); if (@self::$instance[$type]->connect_errno) { echo self::$last_err = "connect failed"; } } } static function fetch_assoc($query,$type){ $db_query = self::run_query($query,$type); $rows = array(); while($row = @$db_query->fetch_assoc()){ $rows[] = $row; } $db_query->free(); return($rows); } static function escape($type,$value){ $value = self::$instance[$type]->real_escape_string($value); return($value); } static function run_query($query,$type){ self::$instance[$type]->ping(); $db_query = self::$instance[$type]->query($query); if(self::$instance[$type]->error){ echo self::$last_err = self::$instance[$type]->error;echo "<p>$query, $type</p>"; return; } return($db_query) ; } static function num_rows($query,$type){ $db_query = self::run_query($query,$type); $num_rows = $db_query->num_rows; return($num_rows); } static function disconnect($type){ @self::$db_obj[$type]->close(); } } ?>
please have @ pdo.
it unifier database object exposing common , effective interface.
it supports server types other mysql too.
even using plainly satisfactory.
Comments
Post a Comment