1 <?php
2
3 class Db{
4
5 private $_host;
6 private $_port;
7 private $_dbname;
8 private $_user;
9 private $_pwd;
10 private $_charset;
11 private $_link;
12 private static $_instance;
13
14 private function __construct($arr = array()){
15
16 $this->_host = $arr['host'] ? $arr['host'] : '127.0.0.1';
17 $this->_port = $arr['port'] ? $arr['port'] : '3306';
18 $this->_user = $arr['user'] ? $arr['user'] : 'root';
19 $this->_pwd = $arr['pwd'] ? $arr['pwd'] : '';
20 $this->_dbname = $arr['dbname'] ? $arr['dbname'] : 'user';
21 $this->_charset = $arr['charset'] ? $arr['charset'] : 'utf8';
22 $this->connect();
23 $this->setCharset();
24
25 }
26
27 /**
28 * 建立数据库连接
29 */
30 private function connect(){
31
32 $this->_link = mysqli_connect($this->_host,$this->_user,$this->_pwd,$this->_dbname,$this->_port);
33
34 if (!$this->_link) {
35 die('数据库连接失败,'. mysqli_connect_error());
36 }
37 }
38
39 /**
40 * 设置字符集
41 */
42 private function setCharset(){
43 mysqli_set_charset($this->_link, $this->_charset);
44 }
45
46 /**
47 * 提供单例对象
48 */
49 public static function getInstance($config = array()){
50
51 if (is_null(self::$_instance)) {
52 self::$_instance = new Db($config);
53 }
54
55 return self::$_instance;
56 }
57
58 /**
59 * 新增数据
60 * @param String $sql 需要执行的SQL
61 * @return mixed 成功返回自增ID,错误返回false
62 */
63 public function insert($sql){
64
65 $this->query($sql);
66
67 return mysqli_affected_rows($this->_link) ? mysqli_insert_id($this->_link) : false;
68 }
69
70 /**
71 * 修改数据
72 * @param String $sql 需要执行的SQL
73 * @return mixed 成功返回影响行数,错误返回false
74 */
75 public function upate($sql){
76
77 $this->query($sql);
78
79 return mysqli_affected_rows($this->_link) ? mysqli_affected_rows($this->_link) : false;
80 }
81
82 /**
83 * 删除数据
84 * @param String $sql 需要执行的SQL
85 * @return mixed 成功返回影响行数,错误返回false
86 */
87 public function delete($sql){
88
89 $this->query($sql);
90
91 return mysqli_affected_rows($this->_link) ? mysqli_affected_rows($this->_link) : false;
92 }
93
94 /**
95 * 获取单条数据
96 * @param String $sql 需要执行的SQL
97 * @return mixed 成功返回数组,错误返回false
98 */
99 public function getOne($sql){
100
101 $result = $this->query($sql);
102
103 return mysqli_num_rows($result) ? mysqli_fetch_assoc($result) : false;
104 }
105
106 /**
107 * 获取多条数据
108 * @param String $sql 需要执行的SQL
109 * @param String $keyFiled 下标关键字
110 * @return mixed 成功返回数组,错误返回false
111 */
112 public function getAll($sql,$keyFiled=null){
113
114 $result = $this->query($sql);
115
116 if (mysqli_num_rows($result)) {
117
118 $list = array();
119
120 while ($rows = mysqli_fetch_assoc($result)) {
121 if (isset($keyFiled)) {
122 $list[$rows[$keyFiled]] = $rows;
123 }else{
124 $list[] = $rows;
125 }
126 }
127
128 return $list;
129 }
130
131 return false;
132 }
133
134 /**
135 * 事务默认不自动提交
136 */
137 private function autocommit(){
138
139 mysqli_autocommit($this->_link,false);
140 }
141
142 /**
143 * 开启事务
144 */
145 public function begin_transaction(){
146
147 $this->autocommit();
148
149 mysqli_begin_transaction($this->_link);
150
151 }
152
153 /**
154 * 事务提交
155 */
156 public function commit(){
157
158 mysqli_commit($this->_link);
159 }
160
161 /**
162 * 事务回滚
163 */
164 public function rollback(){
165
166 mysqli_rollback($this->_link);
167 }
168
169 /**
170 * 执行SQL操作
171 * @param String $sql 需要执行的SQL
172 * @return mixed 只要不出错,全部返回
173 */
174 private function query($sql){
175
176 $res = mysqli_query($this->_link,$sql);
177
178 if (!$res) {
179 die('SQL执行出现错误'. mysqli_error($this->_link));
180 }
181
182 return $res;
183 }
184
185 /**
186 * 避免子类重载或使用clone关键字
187 */
188 private function __clone(){}
189 }
190
191
192 ?>