|
1 <?php |
|
2 /*********************************************************************** |
|
3 |
|
4 Copyright (C) 2002-2005 Rickard Andersson (rickard@punbb.org) |
|
5 |
|
6 This file is part of PunBB. |
|
7 |
|
8 PunBB is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published |
|
10 by the Free Software Foundation; either version 2 of the License, |
|
11 or (at your option) any later version. |
|
12 |
|
13 PunBB is distributed in the hope that it will be useful, but |
|
14 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program; if not, write to the Free Software |
|
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
|
21 MA 02111-1307 USA |
|
22 |
|
23 ************************************************************************/ |
|
24 |
|
25 |
|
26 // Make sure we have built in support for MySQL |
|
27 if (!function_exists('mysql_connect')) |
|
28 exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.'); |
|
29 |
|
30 |
|
31 class DBLayer |
|
32 { |
|
33 var $prefix; |
|
34 var $link_id; |
|
35 var $query_result; |
|
36 |
|
37 var $saved_queries = array(); |
|
38 var $num_queries = 0; |
|
39 |
|
40 |
|
41 function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) |
|
42 { |
|
43 $this->prefix = $db_prefix; |
|
44 |
|
45 if ($p_connect) |
|
46 $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password); |
|
47 else |
|
48 $this->link_id = @mysql_connect($db_host, $db_username, $db_password); |
|
49 |
|
50 if ($this->link_id) |
|
51 { |
|
52 if (@mysql_select_db($db_name, $this->link_id)) |
|
53 return $this->link_id; |
|
54 else |
|
55 error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
|
56 } |
|
57 else |
|
58 error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__); |
|
59 } |
|
60 |
|
61 |
|
62 function start_transaction() |
|
63 { |
|
64 return; |
|
65 } |
|
66 |
|
67 |
|
68 function end_transaction() |
|
69 { |
|
70 return; |
|
71 } |
|
72 |
|
73 |
|
74 function query($sql, $unbuffered = false) |
|
75 { |
|
76 if (defined('PUN_SHOW_QUERIES')) |
|
77 $q_start = get_microtime(); |
|
78 |
|
79 if ($unbuffered) |
|
80 $this->query_result = @mysql_unbuffered_query($sql, $this->link_id); |
|
81 else |
|
82 $this->query_result = @mysql_query($sql, $this->link_id); |
|
83 |
|
84 if ($this->query_result) |
|
85 { |
|
86 if (defined('PUN_SHOW_QUERIES')) |
|
87 $this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start)); |
|
88 |
|
89 ++$this->num_queries; |
|
90 |
|
91 return $this->query_result; |
|
92 } |
|
93 else |
|
94 { |
|
95 if (defined('PUN_SHOW_QUERIES')) |
|
96 $this->saved_queries[] = array($sql, 0); |
|
97 |
|
98 return false; |
|
99 } |
|
100 } |
|
101 |
|
102 |
|
103 function result($query_id = 0, $row = 0) |
|
104 { |
|
105 return ($query_id) ? @mysql_result($query_id, $row) : false; |
|
106 } |
|
107 |
|
108 |
|
109 function fetch_assoc($query_id = 0) |
|
110 { |
|
111 return ($query_id) ? @mysql_fetch_assoc($query_id) : false; |
|
112 } |
|
113 |
|
114 |
|
115 function fetch_row($query_id = 0) |
|
116 { |
|
117 return ($query_id) ? @mysql_fetch_row($query_id) : false; |
|
118 } |
|
119 |
|
120 |
|
121 function num_rows($query_id = 0) |
|
122 { |
|
123 return ($query_id) ? @mysql_num_rows($query_id) : false; |
|
124 } |
|
125 |
|
126 |
|
127 function affected_rows() |
|
128 { |
|
129 return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false; |
|
130 } |
|
131 |
|
132 |
|
133 function insert_id() |
|
134 { |
|
135 return ($this->link_id) ? @mysql_insert_id($this->link_id) : false; |
|
136 } |
|
137 |
|
138 |
|
139 function get_num_queries() |
|
140 { |
|
141 return $this->num_queries; |
|
142 } |
|
143 |
|
144 |
|
145 function get_saved_queries() |
|
146 { |
|
147 return $this->saved_queries; |
|
148 } |
|
149 |
|
150 |
|
151 function free_result($query_id = false) |
|
152 { |
|
153 return ($query_id) ? @mysql_free_result($query_id) : false; |
|
154 } |
|
155 |
|
156 |
|
157 function escape($str) |
|
158 { |
|
159 if (is_array($str)) |
|
160 return ''; |
|
161 else if (function_exists('mysql_real_escape_string')) |
|
162 return mysql_real_escape_string($str, $this->link_id); |
|
163 else |
|
164 return mysql_escape_string($str); |
|
165 } |
|
166 |
|
167 |
|
168 function error() |
|
169 { |
|
170 $result['error_sql'] = @current(@end($this->saved_queries)); |
|
171 $result['error_no'] = @mysql_errno($this->link_id); |
|
172 $result['error_msg'] = @mysql_error($this->link_id); |
|
173 |
|
174 return $result; |
|
175 } |
|
176 |
|
177 |
|
178 function close() |
|
179 { |
|
180 if ($this->link_id) |
|
181 { |
|
182 if ($this->query_result) |
|
183 @mysql_free_result($this->query_result); |
|
184 |
|
185 return @mysql_close($this->link_id); |
|
186 } |
|
187 else |
|
188 return false; |
|
189 } |
|
190 } |