1 <?php
  2 //类名,也习惯上(推荐)使用跟文件名相似的名字
  3 //定义一个mysql连接类,该类可以连接mysql数据库
  4 //并实现其单例模式
  5 //该类的功能还能够完成如下基本mysql操作:
  6 //执行普通的增删改非返回结果集的语句
  7 //执行select语句并可以返回3种类型的数据:
  8 //多行结果(二维数组),单行结果(一维数组)
  9 //单行单列(单个数据)
 10 class MySQLDB{
 11     public $host;
 12     public $port;
 13     public $username;
 14     public $password;
 15     public $charset;
 16     public $dbname;
 17 
 18     //连接结果(资源)
 19     private static $link;
 20 
 21     private $resourc;
 22 //下面的这个就是单例模式所加的函数,为啥加这个函数,就是叫这个类只是实例化一次就够了。
 23     public static function getInstance($config){
 24         if(!isset(self::$link)){
 25             self::$link = new self($config);
//为啥self后面的括号里面加上$config,因为要和那个__construct函数的传的变量是一致的,你是实例化类不就是调用那个__construct函数一次嘛,所以啊,实例化对象就是调用__construct函数。
 26         }
 27         return self::$link;
 28     }
 29     
 30     //构造函数:禁止new
 31     private function __construct($config){
 32         //初始化数据
 33         $this->host = isset($config['host']) ? $config['host'] : 'localhost';
 34         $this->port = isset($config['port']) ? $config['port'] : '3306';
 35         $this->username = isset($config['username']) ? $config['username'] : 'root';
 36         $this->password = isset($config['password']) ? $config['password'] : '';
 37         $this->charset = isset($config['charset']) ? $config['charset'] : 'utf8';
 38         $this->dbname = isset($config['dbname']) ? $config['dbname'] : '';
 39 
 40         //连接数据库
 41         $this->connect();
 42         //设定连接编码
 43         $this->setCharset($this->charset);
 44         //选定数据库
 45         $this->selectDb($this->dbname);
 46     }
 47     //禁止克隆,禁止克隆,禁止克隆,就是将这个方法变成一个私有的,这样的在类的外面不就不能调用那个模数方法了,那么也就不能克隆对象了。
 48     private function __clone(){}
 49     //这里进行连接
 50     public function connect(){
 51         $this->resourc = mysql_connect("$this->host:$this->port", "$this->username","$this->password") or die("连接数据库失败!");
 52     }
 53     public function setCharset($charset){
 54         //mysql_set_charset($charset, $this->resourc); 
 55         $this->query("set names $charset");
 56     }
 57     public function selectDb($dbname){
 58         //mysql_select_db($dbname, $this->resourc);
 59         $this->query("use $dbname;") ;
 60     }
 61 
 62     /**
 63      * 功能:执行最基本(任何)sql语句
 64      * 返回:如果失败直接结束,如果成功,返回执行结果
 65      */
 66     public function query($sql){
 67         if(!$result = mysql_query($sql, $this->resourc))
 68         {
 69             echo ("<br />执行失败。");
 70             echo "<br />失败的sql语句为:" . $sql;
 71             echo "<br />出错信息为:" . mysql_error();
 72             echo "<br />错误代号为:" . mysql_errno();
 73             die();
 74         }
 75         return $result;
 76     }
 77     /**
 78      * 功能:执行select语句,返回2维数组
 79      * 参数:$sql 字符串类型 select语句
 80      */
 81     public function getAll($sql){
 82         $result = $this->query($sql);
 83         $arr = array();    //空数组
 84         while( $rec = mysql_fetch_assoc( $result )){
 85             $arr[] = $rec;//这样就形成二维数组
 86         }
 87         return $arr;
 88     }
 89     //返回一行数据(作为一维数组)
 90     public function getRow($sql){
 91         $result = $this->query($sql);
 92         //$rec = array();
 93         if( $rec2 = mysql_fetch_assoc( $result )){//返回下标为字段名的数组
 94             //如果fetch出来有数据(也就是取得了一行数据),结果自然是数组
 95             return $rec2;
 96         }
 97         return false;
 98     }
 99     //返回一个数据(select语句的第一行第一列)
100     //比如常见的:select count(*) as c from XXX where ...
101     public function getOne($sql){
102         $result = $this->query($sql);
103         $rec = mysql_fetch_row($result);//返回下标为数字的数组,且下标一定是0,1,2, 3.....
104                                         //如果没有数据,返回false
105         if($rec === false){
106             return false;
107         }
108         return $rec[0];    //该数组的第一项。
109 
110     }
111 
112 }