php单例模式连接数据 并查询完整类

<?php
class DB{

    static private $_instance;
    static private $_connectSource;
    private $_dbConfig=array(
        'host'=>'127.0.0.1',
        'user'=>'root',
        'password'=>'',
        'database'=>'cms'

    );
    private function _construct(){

    }

    static public function getInstacne(){
        if(!(self::$_instance instanceof self)){
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function connect(){
        if(!self::$_connectSource){
            self::$_connectSource = mysql_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']);
            if(!self::$_connectSource){
                die('mysql conncet error'.mysql_error());
            }
            mysql_select_db($this->_dbConfig['database'], self::$_connectSource);
            mysql_query("set names UTF8", self::$_connectSource);
        }

        return  self::$_connectSource;
    }

}

 $connect = DB::getInstacne()->connect();
$sql="SELECT 'username' FROM `v9_admin` LIMIT 0 , 30";
$res = mysql_query($sql,$connect);
$row = mysql_fetch_array($res);
var_dump($row);

 

posted on 2015-04-16 16:28  < Angus >  阅读(122)  评论(0编辑  收藏  举报

导航