Db::connect PHP Method

connect() public method

连接数据库方法
public connect ( $config = '', $linkNum )
    public function connect($config = '', $linkNum = 0)
    {
        if (!isset($this->linkID[$linkNum])) {
            if (empty($config)) {
                $config = $this->config;
            }
            // 处理不带端口号的socket连接情况
            $host = $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');
            if ($this->pconnect) {
                $this->linkID[$linkNum] = mysql_pconnect($host, $config['username'], $config['password'], CLIENT_MULTI_RESULTS);
            } else {
                $this->linkID[$linkNum] = mysql_connect($host, $config['username'], $config['password'], true, CLIENT_MULTI_RESULTS);
            }
            if (!$this->linkID[$linkNum] || !empty($config['database']) && !mysql_select_db($config['database'], $this->linkID[$linkNum])) {
                throw_exception(mysql_error());
            }
            $dbVersion = mysql_get_server_info($this->linkID[$linkNum]);
            if ($dbVersion >= '4.1') {
                //使用UTF8存取数据库 需要mysql 4.1.0以上支持
                mysql_query("SET NAMES '" . C('DB_CHARSET') . "'", $this->linkID[$linkNum]);
            }
            //设置 sql_model
            if ($dbVersion > '5.0.1') {
                mysql_query("SET sql_mode=''", $this->linkID[$linkNum]);
            }
            // 标记连接成功
            $this->connected = true;
            // 注销数据库连接配置信息
            if (1 != C('DB_DEPLOY_TYPE')) {
                unset($this->config);
            }
        }
        return $this->linkID[$linkNum];
    }

Usage Example

 function __construct()
 {
     include 'files/config.php';
     $msql = new Db();
     $msql->connect();
     $this->is_login();
 }
All Usage Examples Of Db::connect