/** * @param string $which * @return db_Mysql */ public static function db($which = 'master') { $db_config = Yaf_Registry::get('config')->get('yaf')->get('db')->{$which}; $cache_config = Yaf_Registry::get('config')->get('yaf')->get('cache'); $db = db_Mysql::getInstance($db_config, $cache_config); return $db; // return ($db instanceof db_DbInterface) ? $db : false; }
/** * 单例连接数据库 * @param \OB|string $db_config * @param \OB|string $cache_config * @return db_Mysql */ public static function getInstance($db_config = '', $cache_config = '') { $_db_host = $db_config->host; $_db_port = $db_config->port; $_db_name = $db_config->dbname; $_db_charset = $db_config->charset; $_db_usr = $db_config->usr; $_db_pwd = $db_config->pwd; $_cache_system = $cache_config->system; $_cache_type = $cache_config->type; $_cache_host = $cache_config->host; $_cache_port = $cache_config->port; self::$_database_name = $_db_name; $idx = md5($_db_host . $_db_name . $_cache_host . $_cache_port); if (!isset(self::$_instances[$idx])) { self::$_instances[$idx] = new self($_db_host, $_db_port, $_db_usr, $_db_pwd, $_db_name, $_db_charset, $_cache_system, $_cache_type, $_cache_host, $_cache_port); } return self::$_instances[$idx]; }
function __destruct() { if ($this->db) { $this->db->close(); } }
<?php require_once 'include/sql_class.php'; $db = new db_Mysql(); $db->dbServer = 'localhost'; $db->dbbase = 'messageboard'; $db->dbUser = '******'; $db->dbPwd = ''; $db->dbconnect(); define('MCBOOKINSTALLED', true); define('TABLE_PREFIX', "mb_"); if (PHP_VERSION > '5.2.0') { date_default_timezone_set('PRC'); }
function db_query_mysql($sql, &$res, $timeout = 0, $type = '0', $key = null) { #echo "MC:".($type)."#<br>"; global $dbhost, $dbuser, $dbpassword, $dbname; $db = new db_Mysql(); $db->init_con($dbhost, $dbuser, $dbpassword, $dbname); $db->create_con(); $result = $db->query($sql); if ($result == false) { $db->free(); $db->close(); return false; } else { $res = array(); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { $res[] = $row; } global $memcached_host; $options = array('servers' => array($memcached_host), 'debug' => false, 'compress_threshold' => 510240, 'persistant' => false); $mc = new memcached($options); if ($type == "1") { //add type $mc->add($key, $res, $timeout); } if ($type == "2") { //replace $mc->replace($key, $res, $timeout); } $mc->disconnect_all(); } $db->free(); $db->close(); return true; }