]>
src.bluestatic.org Git - isso.git/blob - db_mysql.php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
14 * MySQL Database Abstraction Layer
20 $OBJECT = 'MySQL Database Abstraction Layer';
21 $CLASS = 'MySQL_Database_Driver';
25 * MySQL Database Abstraction Layer
27 * This framework is a function wrapper for MySQL functions so we can have
28 * better error reporting and query reporting.
30 * @author Iris Studios, Inc.
31 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
36 class MySQL_Database_Driver
39 * Database that is used in the application
46 * Determines whether or not errors should be shown
53 * Current error number
60 * Description of current error
67 * Currend open MySQL connexion
81 * Current query string
88 * History of all executed queryies
91 var $history = array();
94 * Connect to a the specified database
96 * @param str Server name
97 * @param str User name
99 * @param bool Use p-connect?
101 * @return bool Result of connect
103 function connect($server, $user, $password, $pconnect)
105 if ($this->link_id
== 0)
109 $this->link_id
= @mysql_pconnect($server, $user, $password);
113 $this->link_id
= @mysql_connect($server, $user, $password);
116 if ($this->link_id
== false)
118 $this->error('Link-Id == false, cannot connect');
122 if (@mysql_select_db($this->database
, $this->link_id
))
128 $this->error('Cannot use the database \'' . $this->database
. '\'');
135 * Send a query to MySQL
137 * @param str Query string
139 * @return res Resource ID for query
141 function query($query_str)
143 $this->query_id
= @mysql_query($query_str, $this->link_id
);
144 $this->query_str
= $query_str;
145 $this->history
[] = $this->query_str
;
147 if (defined('ISSO_SHOW_QUERIES_LIVE'))
149 if (constant('ISSO_SHOW_QUERIES_LIVE'))
151 print($this->query_str
. '<hr />');
155 if (!$this->query_id
)
157 $this->error('Invalid SQL query');
160 return $this->query_id
;
164 * Escape a string depending on character set
166 * @param str String to be escaped
168 * @return str Escaped string
170 function escape_string($string)
172 return @mysql_real_escape_string($string, $this->link_id
);
176 * Fetch the query result as an array
178 * @param res Query ID
180 * @return array A row of the query result
182 function fetch_array($query_id)
184 return @mysql_fetch_array($query_id, MYSQL_ASSOC
);
188 * Fetch the query result as an object
190 * @param res Query ID
192 * @return obj An object with the query result
194 function fetch_object($query_id)
196 return @mysql_fetch_object($query_id);
200 * Free the current MySQL query result
202 * @param res Query ID
204 function free_result($query_id)
206 @mysql_free_result($query_id);
208 $this->query_str
= '';
212 * Send a MySQL query and return the first row of the results
214 * @param str Query string
215 * @param str Result return function (in the database layer)
217 * @return mixed Results in variable formats
219 function query_first($query_str, $callback = 'fetch_array')
221 $resource = $this->query($query_str);
222 $return = $this->$callback($resource);
223 $this->free_result($resource);
228 * Fetch the unique ID of the record just inserted
230 * @return int MySQL insert ID
234 return @mysql_insert_id($this->link_id
);
238 * Fetch the number of rows in the result
240 * @param res Query ID
242 * @return int Number of MySQL rows
244 function num_rows($query_id)
246 return @mysql_num_rows($query_id);
250 * Fetch the number of rows affected by the query
252 * @param res Query ID
254 * @return int Number of affected rows
256 function affected_rows($query_id)
258 return @mysql_affected_rows($this->link_id
);
262 * MySQL error wrapper for ISSO::_message()
264 * @param str User defined error message
266 function error($message)
274 $this->error_no
= mysql_errno($this->link_id
);
275 $this->error_str
= mysql_error($this->link_id
);
278 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
280 $message_prepped = "<blockquote>\n<p>";
281 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str
."</pre>\n<br />";
282 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no
. "</span>\n<br />";
283 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str
. "</span>\n<br />";
284 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
285 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
286 $message_prepped .= "\n</p>\n</blockquote>";
288 $_isso->_message('Database Error in `<em>' . $_isso->application
. '</em>`', $message_prepped, 3);
294 /*=====================================================================*\
295 || ###################################################################
298 || ###################################################################
299 \*=====================================================================*/