自己练习写的单例模式的数据库操作类
1 <?php
2 //先建立单模:三私(私有变量、私有构造函数、私有的__clone函数)一公(公有方法),再封装连接数据库,以及返回结果
3 class MySQLDB{
4 //定义连接数据库需要用到的私有属性
5 private $host; //主机id
6 private $port; //端口号
7 private $user; //用户
8 private $psw; //密码
9 private $dbname; //数据库名称
10 private $charset; //字符集
11
12 //定义静态变量保存当前类的实例
13 private static $instance;
14 //防止在外部实例化
15 private function __construct($config){
16 $this -> initParam($config);//初始化参数-----实例化时自调用
17 $this -> initConn();//连接数据库-------因为已经初始化了,现在所有参数已经是我们需要的参数了,所以不需要再带参数了
18 $this -> initDB();//选择数据库
19 $this -> initCharset();//选择字符集
20 }
21
22 //防止在外部克隆
23 private function __clone(){
24
25 }
26 //通过静态公有的方法获取这个类的实例
27 public static function getInstance($config){
28 //当前对象不属于当前例就实例化,也就是静态变量在当前类中只能实例化一次,若是第一次实例化就实例化,若第二次实例化就返回一个当前的实例值。
29 if (!self::$instance instanceof self) {
30 self::$instance = new self($config);
31 }
32 return self::$instance;
33 }
34 //初始化成员变量
35 private function initParam($config){
36 $this -> host = isset($config['host']) ? $config['host'] : 'localhost';
37 $this -> port = isset($config['port']) ? $config['port'] : 3306;
38 $this -> user = isset($config['user']) ? $config['user'] : 'root';
39 $this -> psw = isset($config['psw']) ? $config['psw'] : '';
40 $this -> dbname = isset($config['dbname']) ? $config['dbname'] : '';
41 $this -> charset = isset($config['charset']) ? $config['charset'] : 'utf8';
42 }
43 //连接数据库
44 private function initConn(){
45 $link = mysql_connect("{$this -> host}:{$this -> port}" , $this->user ,$this->psw);
46 //判断数据库连接是否成功
47 if (!$link) {
48 echo "mysql连接错误,错误原因如下:<br />";
49 echo "错误代码:".mysql_errno().'<br />';
50 echo "错误信息:".mysql_error();
51 exit;
52 }
53 }
54 //选择数据库
55 private function initDB(){
56 //mysql_select_db('{$this -> dbname}');
57 $this -> query("use `{$this -> dbname}`");
58 }
59 //选择字符集
60 private function initCharset(){
61 //mysql_query('set names `{$this -> charset}`');
62 $this->query("set names {$this->charset}");
63 }
64 //封装一个执行SQL语句的方法
65 /**
66 * @param $sql string 执行的SQL语句
67 * @return $result 如果是数据查询语句返回结果集,如果是数据操作语句返回true,false
68 */
69 private function query($sql){
70 if (!$result = mysql_query($sql)) {
71 echo 'SQL语句执行失败。<br />';
72 echo '错误号是:'.mysql_errno().'<br />';
73 echo '错误信息:'.mysql_error().'<br />';
74 echo '错误的SQL语句是:'.$sql.'<br />';
75 exit;
76 }
77 return $result;
78 }
79
80 /**
81 * 获得所有的数据
82 * @param $sql string 执行SQL
83 * @param $type string 匹配的类型 assoc||array||row
84 * @return $result array 返回值为二维数组
85 */
86 public function fetchAll($sql,$type = 'assoc'){
87 $res = $this -> query($sql); //返回的是资源的结果集
88 //定义一个数组来保存允许匹配的类型
89 $allow_array = array('assoc','array','row');
90 if (!in_array($type, $allow_array)) {
91 $type = 'assoc';
92 }
93 $fn = 'mysql_fetch_'.$type;
94 //将一条结果转成二维数组
95 $result = array();
96 while ($rows = $fn($res)) {
97 $result[] = $rows;
98 }
99 return $result;
100 }
101
102 /**
103 * * 获取一条数据
104 * @param $sql string 执行SQL
105 * @param $type string 匹配的类型 assoc||array||row
106 * @return $result array 返回值为一维数组
107 */
108 public function fetchRow($sql,$type = 'assoc'){
109 $result = $this -> fetchAll($sql,$type);
110 if(!empty($result)){
111 return $result[0];//返回第一行
112 }
113 return $result;
114
115 }
116
117 /**
118 * 获取一行一列
119 * @param $sql string 执行SQL
120 * @param $type string 匹配的类型 assoc||array||row
121 * @return $result string 返回值为一行一列
122 */
123 public function fetchColumn($sql){
124 $result=$this->fetchRow($sql,'row');
125 if(!empty($result)){
126 return $result[0]; //返回一行一列
127 }
128 return $result;
129 }
130
131
132
133 }
134 //设置响应头,防止乱码
135 header('Content-type:text/html;charset=utf-8');
136 $config = array(
137 'host' => 'localhost',
138 'port' => 3306,
139 'user' => 'root',
140 'psw' => 'mysql',
141 'dbname' => 'oop',
142 'charset' => 'utf8'
143 );
144 $db = MySQLDB::getInstance($config);//获取对象的实例
145 $rs = $db -> fetchAll('select * from star');//自己选择要执行的SQL语句
146 echo '<pre>';
147 var_dump($rs);
148
149
150
151 ?>