Hệ thống xếp lịch học tín chỉ cho sinh viên CNTT trên PHP & MySQL
112.917 lượt xem;
- db.php
- project /
1 <?php
2
3 /*
4 You can implement a new db driver in this file by uncommenting the line below
5 and replacing 'mssql' with the desired driver name, then adding cases for the
6 that driver in each function below ...
7 */
8 // define('DATABASE', 'mssql');
9
10
11
12
13 if(!defined('DATABASE')){
14 if(function_exists('mysqli_connect')){
15 define('DATABASE', 'mysqli');
16 }else{
17 define('DATABASE', 'mysql');
18 }
19 }
20
21
22
23 define('mysql_charset', 'utf8');
24
25 function db_link($link = NULL){
26 static $db_link;
27 if($link) $db_link = $link;
28 return $db_link;
29 }
30
31 function db_close($link = NULL){
32 db_link(false); /* close db link */
33 switch(DATABASE){
34 case 'mysql':
35 return mysql_close($link);
36 case 'mysqli':
37 return mysqli_close($link);
38 }
39 }
40
41 function db_select_db($dbname, $link = NULL){
42 if(!$link) $link = db_link();
43 switch(DATABASE){
44 case 'mysql':
45 return mysql_select_db($dbname, $link);
46 case 'mysqli':
47 return mysqli_select_db($link, $dbname);
48 }
49 }
50
51 function db_fetch_array($res){
52 switch(DATABASE){
53 case 'mysql':
54 return @mysql_fetch_array($res);
55 case 'mysqli':
56 return @mysqli_fetch_array($res, MYSQLI_BOTH);
57 }
58 }
59
60 function db_fetch_assoc($res){
61 switch(DATABASE){
62 case 'mysql':
63 return @mysql_fetch_assoc($res);
64 case 'mysqli':
65 return @mysqli_fetch_assoc($res);
66 }
67 }
68
69 function db_fetch_row($res){
70 switch(DATABASE){
71 case 'mysql':
72 return @mysql_fetch_row($res);
73 case 'mysqli':
74 return @mysqli_fetch_row($res);
75 }
76 }
77
78 function db_num_fields($res){
79 switch(DATABASE){
80 case 'mysql':
81 return @mysql_num_fields($res);
82 case 'mysqli':
83 return @mysqli_num_fields($res);
84 }
85 }
86
87 function db_num_rows($res){
88 switch(DATABASE){
89 case 'mysql':
90 return @mysql_num_rows($res);
91 case 'mysqli':
92 return @mysqli_num_rows($res);
93 }
94 }
95
96 function db_affected_rows($link = NULL){
97 if(!$link) $link = db_link();
98 switch(DATABASE){
99 case 'mysql':
100 return mysql_affected_rows($link);
101 case 'mysqli':
102 return mysqli_affected_rows($link);
103 }
104 }
105
106 function db_query($query, $link = NULL){
107 if(!$link) $link = db_link();
108 switch(DATABASE){
109 case 'mysql':
110 return @mysql_query($query, $link);
111 case 'mysqli':
112 return @mysqli_query($link, $query);
113 }
114 }
115
116 function db_insert_id($link = NULL){
117 if(!$link) $link = db_link();
118 switch(DATABASE){
119 case 'mysql':
120 return mysql_insert_id($link);
121 case 'mysqli':
122 return mysqli_insert_id($link);
123 }
124 }
125
126 function db_field_name($res, $field_offset){
127 switch(DATABASE){
128 case 'mysql':
129 return @mysql_field_name($res, $field_offset);
130 case 'mysqli':
131 $fo = @mysqli_fetch_field_direct($res, $field_offset);
132 if($fo === false) return false;
133 return $fo->name;
134 }
135 }
136
137 function db_field_type($res, $field_offset){
138 switch(DATABASE){
139 case 'mysql':
140 return @mysql_field_type($res, $field_offset);
141 case 'mysqli':
142 $fo = @mysqli_fetch_field_direct($res, $field_offset);
143 if($fo === false) return false;
144 return $fo->type;
145 }
146 }
147
148 function db_escape($str = NULL, $link = NULL){
149 if(!$link) $link = db_link();
150 switch(DATABASE){
151 case 'mysql':
152 if(function_exists('mysql_real_escape_string')) return mysql_real_escape_string($str, $link);
153 return mysql_escape_string($str);
154 case 'mysqli':
155 return mysqli_real_escape_string($link, $str);
156 }
157 }
158
159 function db_connect($host = NULL, $username = NULL, $passwd = NULL, $dbname = NULL, $port = NULL, $socket = NULL){
160 switch(DATABASE){
161 case 'mysql':
162 if($host === NULL) $host = ini_get("mysql.default_host");
163 if($username === NULL) $username = ini_get("mysql.default_user");
164 if($passwd === NULL) $passwd = ini_get("mysql.default_password");
165 $link = mysql_connect($host, $username, $passwd);
166 if(!$link) return false;
167 db_link($link); /* db_link() can now be used to retrieve the db link from anywhere */
168 @mysql_query("SET NAMES '" . mysql_charset . "'");
169 if($dbname){ if(!mysql_select_db($dbname, $link)) return false; }
170 return $link;
171 case 'mysqli':
172 if($host === NULL) $host = ini_get("mysqli.default_host");
173 if($username === NULL) $username = ini_get("mysqli.default_user");
174 if($passwd === NULL) $passwd = ini_get("mysqli.default_pw");
175 if($dbname === NULL) $dbname = "";
176 if($port === NULL) $port = ini_get("mysqli.default_port");
177 if($socket === NULL) $socket = ini_get("mysqli.default_socket");
178 $link = mysqli_connect($host, $username, $passwd, $dbname, $port, $socket);
179 if(!$link) return false;
180 db_link($link); /* db_link() can now be used to retrieve the db link from anywhere */
181 mysqli_set_charset($link, mysql_charset);
182 return $link;
183 }
184 }
185
186 function db_errno($link = NULL, $mysqli_connect = false){
187 if(!$link) $link = db_link();
188 switch(DATABASE){
189 case 'mysql':
190 return mysql_errno($link);
191 case 'mysqli':
192 if($mysqli_connect) return mysqli_connect_errno();
193 return mysqli_errno($link);
194 }
195 }
196
197 function db_error($link = NULL, $mysqli_connect = false){
198 if(!$link) $link = db_link();
199 switch(DATABASE){
200 case 'mysql':
201 return mysql_error($link);
202 case 'mysqli':
203 if($mysqli_connect) return mysqli_connect_error();
204 return mysqli_error($link);
205 }
206 }
2
3 /*
4 You can implement a new db driver in this file by uncommenting the line below
5 and replacing 'mssql' with the desired driver name, then adding cases for the
6 that driver in each function below ...
7 */
8 // define('DATABASE', 'mssql');
9
10
11
12
13 if(!defined('DATABASE')){
14 if(function_exists('mysqli_connect')){
15 define('DATABASE', 'mysqli');
16 }else{
17 define('DATABASE', 'mysql');
18 }
19 }
20
21
22
23 define('mysql_charset', 'utf8');
24
25 function db_link($link = NULL){
26 static $db_link;
27 if($link) $db_link = $link;
28 return $db_link;
29 }
30
31 function db_close($link = NULL){
32 db_link(false); /* close db link */
33 switch(DATABASE){
34 case 'mysql':
35 return mysql_close($link);
36 case 'mysqli':
37 return mysqli_close($link);
38 }
39 }
40
41 function db_select_db($dbname, $link = NULL){
42 if(!$link) $link = db_link();
43 switch(DATABASE){
44 case 'mysql':
45 return mysql_select_db($dbname, $link);
46 case 'mysqli':
47 return mysqli_select_db($link, $dbname);
48 }
49 }
50
51 function db_fetch_array($res){
52 switch(DATABASE){
53 case 'mysql':
54 return @mysql_fetch_array($res);
55 case 'mysqli':
56 return @mysqli_fetch_array($res, MYSQLI_BOTH);
57 }
58 }
59
60 function db_fetch_assoc($res){
61 switch(DATABASE){
62 case 'mysql':
63 return @mysql_fetch_assoc($res);
64 case 'mysqli':
65 return @mysqli_fetch_assoc($res);
66 }
67 }
68
69 function db_fetch_row($res){
70 switch(DATABASE){
71 case 'mysql':
72 return @mysql_fetch_row($res);
73 case 'mysqli':
74 return @mysqli_fetch_row($res);
75 }
76 }
77
78 function db_num_fields($res){
79 switch(DATABASE){
80 case 'mysql':
81 return @mysql_num_fields($res);
82 case 'mysqli':
83 return @mysqli_num_fields($res);
84 }
85 }
86
87 function db_num_rows($res){
88 switch(DATABASE){
89 case 'mysql':
90 return @mysql_num_rows($res);
91 case 'mysqli':
92 return @mysqli_num_rows($res);
93 }
94 }
95
96 function db_affected_rows($link = NULL){
97 if(!$link) $link = db_link();
98 switch(DATABASE){
99 case 'mysql':
100 return mysql_affected_rows($link);
101 case 'mysqli':
102 return mysqli_affected_rows($link);
103 }
104 }
105
106 function db_query($query, $link = NULL){
107 if(!$link) $link = db_link();
108 switch(DATABASE){
109 case 'mysql':
110 return @mysql_query($query, $link);
111 case 'mysqli':
112 return @mysqli_query($link, $query);
113 }
114 }
115
116 function db_insert_id($link = NULL){
117 if(!$link) $link = db_link();
118 switch(DATABASE){
119 case 'mysql':
120 return mysql_insert_id($link);
121 case 'mysqli':
122 return mysqli_insert_id($link);
123 }
124 }
125
126 function db_field_name($res, $field_offset){
127 switch(DATABASE){
128 case 'mysql':
129 return @mysql_field_name($res, $field_offset);
130 case 'mysqli':
131 $fo = @mysqli_fetch_field_direct($res, $field_offset);
132 if($fo === false) return false;
133 return $fo->name;
134 }
135 }
136
137 function db_field_type($res, $field_offset){
138 switch(DATABASE){
139 case 'mysql':
140 return @mysql_field_type($res, $field_offset);
141 case 'mysqli':
142 $fo = @mysqli_fetch_field_direct($res, $field_offset);
143 if($fo === false) return false;
144 return $fo->type;
145 }
146 }
147
148 function db_escape($str = NULL, $link = NULL){
149 if(!$link) $link = db_link();
150 switch(DATABASE){
151 case 'mysql':
152 if(function_exists('mysql_real_escape_string')) return mysql_real_escape_string($str, $link);
153 return mysql_escape_string($str);
154 case 'mysqli':
155 return mysqli_real_escape_string($link, $str);
156 }
157 }
158
159 function db_connect($host = NULL, $username = NULL, $passwd = NULL, $dbname = NULL, $port = NULL, $socket = NULL){
160 switch(DATABASE){
161 case 'mysql':
162 if($host === NULL) $host = ini_get("mysql.default_host");
163 if($username === NULL) $username = ini_get("mysql.default_user");
164 if($passwd === NULL) $passwd = ini_get("mysql.default_password");
165 $link = mysql_connect($host, $username, $passwd);
166 if(!$link) return false;
167 db_link($link); /* db_link() can now be used to retrieve the db link from anywhere */
168 @mysql_query("SET NAMES '" . mysql_charset . "'");
169 if($dbname){ if(!mysql_select_db($dbname, $link)) return false; }
170 return $link;
171 case 'mysqli':
172 if($host === NULL) $host = ini_get("mysqli.default_host");
173 if($username === NULL) $username = ini_get("mysqli.default_user");
174 if($passwd === NULL) $passwd = ini_get("mysqli.default_pw");
175 if($dbname === NULL) $dbname = "";
176 if($port === NULL) $port = ini_get("mysqli.default_port");
177 if($socket === NULL) $socket = ini_get("mysqli.default_socket");
178 $link = mysqli_connect($host, $username, $passwd, $dbname, $port, $socket);
179 if(!$link) return false;
180 db_link($link); /* db_link() can now be used to retrieve the db link from anywhere */
181 mysqli_set_charset($link, mysql_charset);
182 return $link;
183 }
184 }
185
186 function db_errno($link = NULL, $mysqli_connect = false){
187 if(!$link) $link = db_link();
188 switch(DATABASE){
189 case 'mysql':
190 return mysql_errno($link);
191 case 'mysqli':
192 if($mysqli_connect) return mysqli_connect_errno();
193 return mysqli_errno($link);
194 }
195 }
196
197 function db_error($link = NULL, $mysqli_connect = false){
198 if(!$link) $link = db_link();
199 switch(DATABASE){
200 case 'mysql':
201 return mysql_error($link);
202 case 'mysqli':
203 if($mysqli_connect) return mysqli_connect_error();
204 return mysqli_error($link);
205 }
206 }