Updated initial copyright year from 2003 to 2002
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2002-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2002 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $OBJECT = 'MySQL Database Abstraction Layer';
14 $CLASS = 'MySQL_Database_Driver';
15 $OBJ = 'db';
16
17 /**
18 * MySQL Database Abstraction Layer
19 *
20 * This framework is a function wrapper for MySQL functions so we can have
21 * better error reporting and query reporting.
22 *
23 * @author Iris Studios, Inc.
24 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
25 * @version $Revision$
26 *
27 */
28 class MySQL_Database_Driver
29 {
30 /**
31 * Global environment variables
32 *
33 * @var database The name of the MySQL database used
34 * @var errshow Report errors durring execution?
35 * @var errno Current error number
36 * @var errstr Description of the current error
37 * @var link_id Link-ID for current connexion
38 * @var query_id Current query ID
39 * @var query_str Current query string
40 * @var history A list of all query reporting information
41 */
42 var $database = '';
43 var $errshow = true;
44 var $errno = 0;
45 var $errstr = '';
46 var $link_id = 0;
47 var $query_id = 0;
48 var $query_str = '';
49 var $history = array();
50
51 /**
52 * Connect to a the specified database
53 *
54 * @param str Server name
55 * @param str User name
56 * @param str Password
57 * @param bool Use p-connect?
58 *
59 * @return bool Result of connect
60 */
61 function connect($server, $user, $password, $pconnect)
62 {
63 if ($this->link_id == 0)
64 {
65 if ($pconnect)
66 {
67 $this->link_id = @mysql_pconnect($server, $user, $password);
68 }
69 else
70 {
71 $this->link_id = @mysql_connect($server, $user, $password);
72 }
73
74 if ($this->link_id == false)
75 {
76 $this->error('Link-Id == false, cannot connect');
77 return false;
78 }
79
80 if (@mysql_select_db($this->database, $this->link_id))
81 {
82 return true;
83 }
84 else
85 {
86 $this->error('Cannot use the database \'' . $this->database . '\'');
87 return false;
88 }
89 }
90 }
91
92 /**
93 * Send a query to MySQL
94 *
95 * @param str Query string
96 *
97 * @return res Resource ID for query
98 */
99 function query($query_str)
100 {
101 $this->query_id = @mysql_query($query_str, $this->link_id);
102 $this->query_str = $query_str;
103 $this->history[] = $this->query_str;
104
105 if (defined('ISSO_SHOW_QUERIES_LIVE'))
106 {
107 if (constant('ISSO_SHOW_QUERIES_LIVE'))
108 {
109 print($this->query_str . '<hr />');
110 }
111 }
112
113 if (!$this->query_id)
114 {
115 $this->error('Invalid SQL query');
116 }
117
118 return $this->query_id;
119 }
120
121 /**
122 * Escape a string depending on character set
123 *
124 * @param str String to be escaped
125 *
126 * @return str Escaped string
127 */
128 function escape_string($string)
129 {
130 return @mysql_real_escape_string($string, $this->link_id);
131 }
132
133 /**
134 * Fetch the query result as an array
135 *
136 * @param res Query-ID
137 *
138 * @return array A row of the query result
139 */
140 function fetch_array($query_id)
141 {
142 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
143 }
144
145 /**
146 * Fetch the query result as an object
147 *
148 * @param res Query-ID
149 *
150 * @return obj An object with the query result
151 */
152 function fetch_object($query_id)
153 {
154 return @mysql_fetch_object($query_id);
155 }
156
157 /**
158 * Free the current MySQL query result
159 *
160 * @param res Query-ID
161 */
162 function free_result($query_id)
163 {
164 @mysql_free_result($query_id);
165 $this->query_id = 0;
166 $this->query_str = '';
167 }
168
169 /**
170 * Send a MySQL query and return the first row of the results
171 *
172 * @param str Query string
173 * @param str Result return function (in the database layer)
174 *
175 * @return mixed Results in variable formats
176 */
177 function query_first($query_str, $callback = 'fetch_array')
178 {
179 $resource = $this->query($query_str);
180 $return = $this->$callback($resource);
181 $this->free_result($resource);
182 return $return;
183 }
184
185 /**
186 * Fetch the unique ID of the record just inserted
187 *
188 * @return int MySQL insert ID
189 */
190 function insert_id()
191 {
192 return @mysql_insert_id($this->link_id);
193 }
194
195 /**
196 * Fetch the number of rows in the result
197 *
198 * @param res Query-ID
199 *
200 * @return int Number of MySQL rows
201 */
202 function num_rows($query_id)
203 {
204 return @mysql_num_rows($query_id);
205 }
206
207 /**
208 * Fetch the number of rows affected by the query
209 *
210 * @return int Number of affected rows
211 */
212 function affected_rows($query_id)
213 {
214 return @mysql_affected_rows($this->link_id);
215 }
216
217 /**
218 * MySQL error wrapper for ISSO::_message()
219 *
220 * @param str User-defined error message
221 */
222 function error($message)
223 {
224 global $_isso;
225
226 if ($this->errshow)
227 {
228 if ($this->link_id)
229 {
230 $this->errno = mysql_errno($this->link_id);
231 $this->errstr = mysql_error($this->link_id);
232 }
233
234 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
235
236 $message_prepped = "<blockquote>\n<p>";
237 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str ."</pre>\n<br />";
238 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errno . "</span>\n<br />";
239 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr . "</span>\n<br />";
240 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
241 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
242 $message_prepped .= "\n</p>\n</blockquote>";
243
244 $_isso->_message('Database Error in `<em>' . $_isso->application . '</em>`', $message_prepped, 3);
245 exit;
246 }
247 }
248 }
249
250 /*=====================================================================*\
251 || ###################################################################
252 || # $HeadURL$
253 || # $Id$
254 || ###################################################################
255 \*=====================================================================*/
256 ?>