Removing @see declarations
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * MySQL Database Abstraction Layer
24 * db_mysql.php
25 *
26 * @package ISSO
27 */
28
29 $OBJECT = 'MySQL Database Abstraction Layer';
30 $CLASS = 'MySQL_Database_Driver';
31 $OBJ = 'db';
32
33 /**
34 * MySQL Database Abstraction Layer
35 *
36 * This framework is a function wrapper for MySQL functions so we can have
37 * better error reporting and query reporting.
38 *
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
41 * @version $Revision$
42 * @package ISSO
43 *
44 */
45 class MySQL_Database_Driver
46 {
47 /**
48 * Database that is used in the application
49 * @var string
50 */
51 var $database = '';
52
53 /**
54 * Determines whether or not errors should be shown
55 * @var bool
56 */
57 var $errshow = true;
58
59 /**
60 * Current error number
61 * @var integer
62 */
63 var $error_no = 0;
64
65 /**
66 * Description of current error
67 * @var string
68 */
69 var $error_str = '';
70
71 /**
72 * Currend open MySQL connexion
73 * @var resource
74 */
75 var $link_id = null;
76
77 /**
78 * Current query ID
79 * @var integer
80 */
81 var $query_id = null;
82
83 /**
84 * Current query string
85 * @var string
86 */
87 var $query_str = '';
88
89 /**
90 * History of all executed queryies
91 * @var array
92 */
93 var $history = array();
94
95 /**
96 * Connect to a the specified database
97 *
98 * @param string Server name
99 * @param string User name
100 * @param string Password
101 * @param bool Use p-connect?
102 *
103 * @return bool Result of connect
104 */
105 function connect($server, $user, $password, $pconnect)
106 {
107 if ($this->link_id == 0)
108 {
109 if ($pconnect)
110 {
111 $this->link_id = @mysql_pconnect($server, $user, $password);
112 }
113 else
114 {
115 $this->link_id = @mysql_connect($server, $user, $password);
116 }
117
118 if ($this->link_id == false)
119 {
120 $this->error('Link-Id == false, cannot connect');
121 return false;
122 }
123
124 if (@mysql_select_db($this->database, $this->link_id))
125 {
126 return true;
127 }
128 else
129 {
130 $this->error('Cannot use the database \'' . $this->database . '\'');
131 return false;
132 }
133 }
134 }
135
136 /**
137 * Send a query to MySQL
138 *
139 * @param string Query string
140 *
141 * @return integer Resource ID for query
142 */
143 function query($query_str)
144 {
145 $this->query_id = @mysql_query($query_str, $this->link_id);
146 $this->query_str = $query_str;
147 $this->history[] = $this->query_str;
148
149 if (defined('ISSO_SHOW_QUERIES_LIVE'))
150 {
151 if (constant('ISSO_SHOW_QUERIES_LIVE'))
152 {
153 print($this->query_str . '<hr />');
154 }
155 }
156
157 if (!$this->query_id)
158 {
159 $this->error('Invalid SQL query');
160 }
161
162 return $this->query_id;
163 }
164
165 /**
166 * Escape a string depending on character set
167 *
168 * @param string String to be escaped
169 *
170 * @return string Escaped string
171 */
172 function escape_string($string)
173 {
174 return @mysql_real_escape_string($string, $this->link_id);
175 }
176
177 /**
178 * Fetch the query result as an array
179 *
180 * @param integer Query ID
181 *
182 * @return array A row of the query result
183 */
184 function fetch_array($query_id)
185 {
186 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
187 }
188
189 /**
190 * Fetch the query result as an object
191 *
192 * @param integer Query ID
193 *
194 * @return object An object with the query result
195 */
196 function fetch_object($query_id)
197 {
198 return @mysql_fetch_object($query_id);
199 }
200
201 /**
202 * Free the current MySQL query result
203 *
204 * @param integer Query ID
205 */
206 function free_result($query_id)
207 {
208 @mysql_free_result($query_id);
209 $this->query_id = 0;
210 $this->query_str = '';
211 }
212
213 /**
214 * Send a MySQL query and return the first row of the results
215 *
216 * @param string Query string
217 * @param string Result return function (in the database layer)
218 *
219 * @return mixed Results in variable formats
220 */
221 function query_first($query_str, $callback = 'fetch_array')
222 {
223 $resource = $this->query($query_str);
224 $return = $this->$callback($resource);
225 $this->free_result($resource);
226 return $return;
227 }
228
229 /**
230 * Fetch the unique ID of the record just inserted
231 *
232 * @return integer MySQL insert ID
233 */
234 function insert_id()
235 {
236 return @mysql_insert_id($this->link_id);
237 }
238
239 /**
240 * Fetch the number of rows in the result
241 *
242 * @param integer Query ID
243 *
244 * @return integer Number of MySQL rows
245 */
246 function num_rows($query_id)
247 {
248 return @mysql_num_rows($query_id);
249 }
250
251 /**
252 * Fetch the number of rows affected by the query
253 *
254 * @param integer Query ID
255 *
256 * @return integer Number of affected rows
257 */
258 function affected_rows($query_id)
259 {
260 return @mysql_affected_rows($this->link_id);
261 }
262
263 /**
264 * MySQL error wrapper for ISSO::_message()
265 *
266 * @param string User defined error message
267 */
268 function error($message)
269 {
270 global $_isso;
271
272 if ($this->errshow)
273 {
274 if ($this->link_id)
275 {
276 $this->error_no = mysql_errno($this->link_id);
277 $this->error_str = mysql_error($this->link_id);
278 }
279
280 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
281
282 $message_prepped = "<blockquote>\n<p>";
283 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str ."</pre>\n<br />";
284 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no . "</span>\n<br />";
285 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str . "</span>\n<br />";
286 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
287 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
288 $message_prepped .= "\n</p>\n</blockquote>";
289
290 $_isso->_message('Database Error in `<em>' . $_isso->application . '</em>`', $message_prepped, 3);
291 exit;
292 }
293 }
294 }
295
296 /*=====================================================================*\
297 || ###################################################################
298 || # $HeadURL$
299 || # $Id$
300 || ###################################################################
301 \*=====================================================================*/
302 ?>