PHP DB CLASS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
 
 
if(!defined('IN')) die('Access denied');
 
require(ROOT_PATH.'include/config.inc.php');
 
class db_op
{
	protected $link;
	protected $dbhost,$dbuser,$dbpwd,$dbname;
 
	public function db_op($host="",$user="",$pwd="",$name="")
	{
		$this->__construct($host,$user,$pwd,$name);
	}
 
	public function __construct($host="",$user="",$pwd="",$name="")
	{
		$this->link = false;
		$this->dbhost = $host;
		$this->dbuser = $user;
		$this->dbpwd = $pwd;
		$this->dbname = $name;
	}
 
	//建立数据库连接,如果带参数,则以参数为最优先,其次为类中参数,最后为config参数
	public function createConnection($host="",$user="",$pwd="",$name="")
	{
		if($host!=""||$user!=""||$pwd!=""||$name!="")
		{
			$this->connectWithInParameter($host,$user,$pwd,$name);
		}
		else if ($this->dbhost!=""&&$this->dbuser!=""&&$this->dbpwd!=""&&$this->dbname!="")
		{
			$this->connectWithClass();
		}
		else
		{
			$this->connectWithConfig();
		}
	}
 
	//由获取的参数建立连接
	protected function connectWithInParameter($host,$user,$pwd,$name)
	{
		if($host!=""||$user!=""||pwd!=""||$name!="")
		{
			$this->halt("连接数据库参数错误");
		}
		else
		{
			$this->link = @mysql_connect($host,$user,$pwd) or die("Can not connect DateBase!");
			@mysql_select_db($name,$this->link) or die("Can not found DataBase!");
			$gb="set names utf8";
			mysql_query($gb);
		}
	}
 
	//由类中参数连接数据库
	protected function connectWithClass()
	{
		$this->link = @mysql_connect($this->dbhost,$this->dbuser,$this->dbpwd) or die("Can not connect DateBase!");
		@mysql_select_db($this->dbname,$this->link) or die("Can not found DataBase!");
		$gb="set names utf8";
		mysql_query($gb);
	}
 
	//由config文件中的数据建立连接
	protected function connectWithConfig()
	{
		$this->link=@mysql_connect(DB_HOST,DB_USER,DB_PW) or die('Can not connect DateBase!');
		@mysql_select_db(DB_NAME,$this->link) or die('Can Not found the DateBase');
		$gb="set names utf8";
		mysql_query($gb);
	}
 
	public function query($sql)
	{
			return mysql_query($sql);
	}
 
	//取得单个记录	
	public function ExecuteScalar($sql)
	{
		$query = $this->query($sql);
		$rs = mysql_fetch_array($query);
		return $rs[0];
	}
 
	//获取全部记录
	public function get_all($sql)
	{
		try 
		{
			$query = $this->query($sql);
			$result=array();
			while ($rs=mysql_fetch_array($query))
			{
				$result[] = $rs;
			}
			return  $result;
		}
		catch (Exception $e)
		{
			$this->halt($e->getMessage());	
		}
	}
 
	public function fetch_array($sql)
	{
		try 
		{
			$query = $this->query($sql);
			return  mysql_fetch_array($query);
		}
		catch (Exception  $e)
		{
			$this->halt($e->getMessage());
		}
 
	}
 
	public function select($table,$keys,$where='',$order='ASC',$start='',$end='')
	{
		$sql = "SELECT ";
		$keys_temp = '';
		$first_key='';
		foreach ($keys as $k=>$v)
		{
				$keys_temp.='`'.addslashes(trim($v)).'`,';
				if ($first_key=='')
				{
					$first_key = addslashes(trim($k));
				}
		}
 
		$keys_temp = substr($keys_temp,0,-1);
		$sql.=' '.$keys_temp.' FROM `'.$table.'` ';
		if ($where!='')
		{
			$sql.=' WHERE '.$where.' ';	
		}
		if($order=='ASC')
		{
			$sql.=' ORDER BY `'.addslashes(trim($keys[$first_key])).'` ';
		}
		else 
		{
			$sql.=' ORDER BY `'.addslashes(trim($keys[$first_key])).'` DESC ';
		}
		if($start!='')
		{
			$sql.=' LIMIT '.$start;
		}
		if($end!='')
		{
			$sql.=','.$end;
		}
 
		//echo $sql;
		return  $this->get_all($sql);
	}
 
	public function insert($table,$insert)
	{
		if (is_array($insert[0]))
		{
			$i=0;
			while (isset($insert[$i]))
			{
				$this->insert($table,$insert[$i]);
				$i++;	
			}
			//echo $i;
		}
 
		$sql = 'INSERT INTO `'.$table.'` (';
		$keys = '';
		$values = '';
 
		foreach ($insert as $k=>$v)
		{
			$keys.='`'.addslashes(trim($k)).'`,';
			$values.='\''.addslashes(trim($v)).'\',';
		}
		$keys = substr($keys,0,-1);
		$values =  substr($values,0,-1);
		$sql.=$keys.') VALUES (';
		$sql.=$values.')';
 
		try
		{
			$this->query($sql);
			$affected_rows = mysql_affected_rows();
 
		    return $affected_rows;
		}
		catch (Exception $e)
		{
			$this->halt($e->getMessage());
		}
		return 0;
	}
 
	public function delete($table,$where)
	{
		$sql="DELETE FROM $table WHERE $where";
		$this->query($sql);
		return $this->affected_rows();
	}
 
	public function update($table,$update,$where='')
	{
		$sql = 'UPDATE `'.$table.'` SET ';
		foreach ($update as $k=>$v)
		{
			$sql.='`'.$k.'`=\''.addslashes(trim($v)).'\',';
		}
		$sql = substr($sql,0,-1);
		if ($where!='') 
		{
			$sql.=' WHERE '.$where;	
		}
 
	    try 
	    {
	    	//echo $sql;
	    	$this->query($sql);
	   		return $this->affected_rows();
	    }
	    catch (Exception $e)
	    {
	    	echo $e->getMessage();
			exit();
	    }
	}
 
 
	public function num_rows($sql)
	{
		$query = $this->query($sql);
		$num = mysql_num_rows($query);
		return  $num;
	}
 
	public function affected_rows()
	{
		return mysql_affected_rows();
	}
 
	public function halt($msg,$url="")
	{
		if($url == "")
		{
			$error = '<strong><center>'.$msg.'</center></strong>';
			exit($error);
		}
		else
		{
			$error = '<strong><center>'.$msg.'</center></strong>';
			$error.='<a href="'.$url.'">'.$url.'</a>';
			exit($error);
		}
	}
}
?>

“PHP DB CLASS”   5 comments

貌似实现的功能不多哦~~
avatar
收下了 谢谢
avatar
好长的类``` 对了,问个问题,那个mysql和mysqli有什么大区别么?(函数方面)
avatar
@这样 不知道我的理解正确不,个人觉得Mysql是过程,而mysqli是封装成类了,以对象的方式! 其实到PHP6默认的是用PDO了~~
avatar

Leave a comment:

XHTML: You can use these tags: <a href="" title=""> <b> <blockquote cite=""> <em> <i> <strong> <p> <br><br />