5 // Justin Vincent (justin@visunet.ie)
6 // http://php.justinvincent.com
8 define('EZSQL_VERSION', 'WP1.25');
9 define('OBJECT', 'OBJECT', true);
10 define('ARRAY_A', 'ARRAY_A', false);
11 define('ARRAY_N', 'ARRAY_N', false);
13 if (!defined('SAVEQUERIES'))
14 define('SAVEQUERIES', false);
18 var $show_errors = true;
36 var $optiongroup_options;
39 // ==================================================================
40 // DB Constructor - connects to the server and selects a database
42 function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
43 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);
46 <h1>Error establishing a database connection</h1>
47 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
49 <li>Are you sure you have the correct username and password?</li>
50 <li>Are you sure that you have typed the correct hostname?</li>
51 <li>Are you sure that the database server is running?</li>
53 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
57 $this->select($dbname);
60 // ==================================================================
61 // Select a DB (if another one needs to be selected)
63 function select($db) {
64 if (!@mysql_select_db($db, $this->dbh)) {
66 <h1>Can’t select database</h1>
67 <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>$db</code> database.</p>
69 <li>Are you sure it exists?</li>
70 <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
72 <p>If you don't know how to setup a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>");
76 // ====================================================================
77 // Format a string correctly for safe insert under all PHP conditions
79 function escape($string) {
80 return addslashes( $string ); // Disable rest for now, causing problems
81 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
82 return mysql_escape_string( $string );
84 return mysql_real_escape_string( $string, $this->dbh );
87 // ==================================================================
88 // Print SQL/DB error.
90 function print_error($str = '') {
92 if (!$str) $str = mysql_error();
94 array ('query' => $this->last_query, 'error_str' => $str);
96 // Is error output turned on or not..
97 if ( $this->show_errors ) {
98 // If there is an error then take note of it
99 print "<div id='error'>
100 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
101 <code>$this->last_query</code></p>
108 // ==================================================================
109 // Turn error handling on or off..
111 function show_errors() {
112 $this->show_errors = true;
115 function hide_errors() {
116 $this->show_errors = false;
119 // ==================================================================
120 // Kill cached query results
123 $this->last_result = null;
124 $this->col_info = null;
125 $this->last_query = null;
128 // ==================================================================
129 // Basic Query - see docs for more detail
131 function query($query) {
136 // Log how the function was called
137 $this->func_call = "\$db->query(\"$query\")";
139 // Keep track of the last query for debug..
140 $this->last_query = $query;
142 // Perform the query via std mysql_query function..
144 $this->timer_start();
146 $this->result = @mysql_query($query, $this->dbh);
147 ++$this->num_queries;
150 $this->queries[] = array( $query, $this->timer_stop() );
152 // If there is an error then take note of it..
153 if ( mysql_error() ) {
154 $this->print_error();
158 if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
159 $this->rows_affected = mysql_affected_rows();
160 // Take note of the insert_id
161 if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
162 $this->insert_id = mysql_insert_id($this->dbh);
164 // Return number of rows affected
165 $return_val = $this->rows_affected;
168 while ($i < @mysql_num_fields($this->result)) {
169 $this->col_info[$i] = @mysql_fetch_field($this->result);
173 while ( $row = @mysql_fetch_object($this->result) ) {
174 $this->last_result[$num_rows] = $row;
178 @mysql_free_result($this->result);
180 // Log number of rows the query returned
181 $this->num_rows = $num_rows;
183 // Return number of rows selected
184 $return_val = $this->num_rows;
190 // ==================================================================
191 // Get one variable from the DB - see docs for more detail
193 function get_var($query=null, $x = 0, $y = 0) {
194 $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
196 $this->query($query);
198 // Extract var out of cached results based x,y vals
199 if ( $this->last_result[$y] ) {
200 $values = array_values(get_object_vars($this->last_result[$y]));
203 // If there is a value return it else return null
204 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
207 // ==================================================================
208 // Get one row from the DB - see docs for more detail
210 function get_row($query = null, $output = OBJECT, $y = 0) {
211 $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
213 $this->query($query);
215 if ( $output == OBJECT ) {
216 return $this->last_result[$y] ? $this->last_result[$y] : null;
217 } elseif ( $output == ARRAY_A ) {
218 return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
219 } elseif ( $output == ARRAY_N ) {
220 return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
222 $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
226 // ==================================================================
227 // Function to get 1 column from the cached result set based in X index
228 // se docs for usage and info
230 function get_col($query = null , $x = 0) {
232 $this->query($query);
234 // Extract the column values
235 for ( $i=0; $i < count($this->last_result); $i++ ) {
236 $new_array[$i] = $this->get_var(null, $x, $i);
241 // ==================================================================
242 // Return the the query as a result set - see docs for more details
244 function get_results($query = null, $output = OBJECT) {
245 $this->func_call = "\$db->get_results(\"$query\", $output)";
248 $this->query($query);
250 // Send back array of objects. Each row is an object
251 if ( $output == OBJECT ) {
252 return $this->last_result;
253 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
254 if ( $this->last_result ) {
256 foreach( $this->last_result as $row ) {
257 $new_array[$i] = (array) $row;
258 if ( $output == ARRAY_N ) {
259 $new_array[$i] = array_values($new_array[$i]);
271 // ==================================================================
272 // Function to get column meta data info pertaining to the last query
273 // see docs for more info and usage
275 function get_col_info($info_type = 'name', $col_offset = -1) {
276 if ( $this->col_info ) {
277 if ( $col_offset == -1 ) {
279 foreach($this->col_info as $col ) {
280 $new_array[$i] = $col->{$info_type};
285 return $this->col_info[$col_offset]->{$info_type};
290 function timer_start() {
291 $mtime = microtime();
292 $mtime = explode(' ', $mtime);
293 $this->time_start = $mtime[1] + $mtime[0];
297 function timer_stop($precision = 3) {
298 $mtime = microtime();
299 $mtime = explode(' ', $mtime);
300 $time_end = $mtime[1] + $mtime[0];
301 $time_total = $time_end - $this->time_start;
305 function bail($message) { // Just wraps errors in a nice header and footer
306 if ( !$this->show_errors )
308 header( 'Content-Type: text/html; charset=utf-8');
310 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
311 <html xmlns="http://www.w3.org/1999/xhtml">
313 <title>WordPress › Error</title>
314 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
315 <style media="screen" type="text/css">
323 font-family: Georgia, "Times New Roman", Times, serif;
332 font-weight: lighter;
345 padding: 5px 5px 5px 20px;
354 <h1 id="logo"><img alt="WordPress" src="http://static.wordpress.org/logo.png" /></h1>
357 echo "</body></html>";
362 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);