root/branches/1.5/wp-includes/wp-db.php

Revision 2598, 9.8 kB (checked in by matt, 4 years ago)

http://mosquito.wordpress.org/view.php?id=1353 - Hat tip MC_incubus

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 //  WordPress DB Class
3
4 //  ORIGINAL CODE FROM:
5 //  Justin Vincent (justin@visunet.ie)
6 //    http://php.justinvincent.com
7
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);
12
13 if (!defined('SAVEQUERIES'))
14     define('SAVEQUERIES', false);
15
16 class wpdb {
17
18     var $show_errors = true;
19     var $num_queries = 0;   
20     var $last_query;
21     var $col_info;
22     var $queries;
23
24     // Our tables
25     var $posts;
26     var $users;
27     var $categories;
28     var $post2cat;
29     var $comments;
30     var $links;
31     var $linkcategories;
32     var $options;
33     var $optiontypes;
34     var $optionvalues;
35     var $optiongroups;
36     var $optiongroup_options;
37     var $postmeta;
38
39     // ==================================================================
40     //    DB Constructor - connects to the server and selects a database
41
42     function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
43         $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword);
44         if (!$this->dbh) {
45             $this->bail("
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>
48 <ul>
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>
52 </ul>
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>
54 ");
55         }
56
57         $this->select($dbname);
58     }
59
60     // ==================================================================
61     //    Select a DB (if another one needs to be selected)
62
63     function select($db) {
64         if (!@mysql_select_db($db, $this->dbh)) {
65             $this->bail("
66 <h1>Can&#8217;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>
68 <ul>
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>
71 </ul>
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>");
73         }
74     }
75
76     // ====================================================================
77     //    Format a string correctly for safe insert under all PHP conditions
78     
79     function escape($str) {
80         return addslashes($str);               
81     }
82
83     // ==================================================================
84     //    Print SQL/DB error.
85
86     function print_error($str = '') {
87         global $EZSQL_ERROR;
88         if (!$str) $str = mysql_error();
89         $EZSQL_ERROR[] =
90         array ('query' => $this->last_query, 'error_str' => $str);
91
92         // Is error output turned on or not..
93         if ( $this->show_errors ) {
94             // If there is an error then take note of it
95             print "<div id='error'>
96             <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
97             <code>$this->last_query</code></p>
98             </div>";
99         } else {
100             return false;   
101         }
102     }
103
104     // ==================================================================
105     //    Turn error handling on or off..
106
107     function show_errors() {
108         $this->show_errors = true;
109     }
110     
111     function hide_errors() {
112         $this->show_errors = false;
113     }
114
115     // ==================================================================
116     //    Kill cached query results
117
118     function flush() {
119         $this->last_result = null;
120         $this->col_info = null;
121         $this->last_query = null;
122     }
123
124     // ==================================================================
125     //    Basic Query    - see docs for more detail
126
127     function query($query) {
128         // initialise return
129         $return_val = 0;
130         $this->flush();
131
132         // Log how the function was called
133         $this->func_call = "\$db->query(\"$query\")";
134
135         // Keep track of the last query for debug..
136         $this->last_query = $query;
137
138         // Perform the query via std mysql_query function..
139         if (SAVEQUERIES)
140             $this->timer_start();
141         
142         $this->result = @mysql_query($query, $this->dbh);
143         ++$this->num_queries;
144
145         if (SAVEQUERIES)
146             $this->queries[] = array( $query, $this->timer_stop() );
147
148         // If there is an error then take note of it..
149         if ( mysql_error() ) {
150             $this->print_error();
151             return false;
152         }
153
154         if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) {
155             $this->rows_affected = mysql_affected_rows();
156             // Take note of the insert_id
157             if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
158                 $this->insert_id = mysql_insert_id($this->dbh);   
159             }
160             // Return number of rows affected
161             $return_val = $this->rows_affected;
162         } else {
163             $i = 0;
164             while ($i < @mysql_num_fields($this->result)) {
165                 $this->col_info[$i] = @mysql_fetch_field($this->result);
166                 $i++;
167             }
168             $num_rows = 0;
169             while ( $row = @mysql_fetch_object($this->result) ) {
170                 $this->last_result[$num_rows] = $row;
171                 $num_rows++;
172             }
173
174             @mysql_free_result($this->result);
175
176             // Log number of rows the query returned
177             $this->num_rows = $num_rows;
178             
179             // Return number of rows selected
180             $return_val = $this->num_rows;
181         }
182
183         return $return_val;
184     }
185
186     // ==================================================================
187     //    Get one variable from the DB - see docs for more detail
188
189     function get_var($query=null, $x = 0, $y = 0) {
190         $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
191         if ( $query )
192             $this->query($query);
193
194         // Extract var out of cached results based x,y vals
195         if ( $this->last_result[$y] ) {
196             $values = array_values(get_object_vars($this->last_result[$y]));
197         }
198
199         // If there is a value return it else return null
200         return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
201     }
202
203     // ==================================================================
204     //    Get one row from the DB - see docs for more detail
205
206     function get_row($query = null, $output = OBJECT, $y = 0) {
207         $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
208         if ( $query )
209             $this->query($query);
210
211         if ( $output == OBJECT ) {
212             return $this->last_result[$y] ? $this->last_result[$y] : null;
213         } elseif ( $output == ARRAY_A ) {
214             return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
215         } elseif ( $output == ARRAY_N ) {
216             return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
217         } else {
218             $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
219         }
220     }
221
222     // ==================================================================
223     //    Function to get 1 column from the cached result set based in X index
224     // se docs for usage and info
225
226     function get_col($query = null , $x = 0) {
227         if ( $query )
228             $this->query($query);
229
230         // Extract the column values
231         for ( $i=0; $i < count($this->last_result); $i++ ) {
232             $new_array[$i] = $this->get_var(null, $x, $i);
233         }
234         return $new_array;
235     }
236
237     // ==================================================================
238     // Return the the query as a result set - see docs for more details
239
240     function get_results($query = null, $output = OBJECT) {
241         $this->func_call = "\$db->get_results(\"$query\", $output)";
242
243         if ( $query )
244             $this->query($query);
245
246         // Send back array of objects. Each row is an object
247         if ( $output == OBJECT ) {
248             return $this->last_result;
249         } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
250             if ( $this->last_result ) {
251                 $i = 0;
252                 foreach( $this->last_result as $row ) {
253                     $new_array[$i] = (array) $row;
254                     if ( $output == ARRAY_N ) {
255                         $new_array[$i] = array_values($new_array[$i]);
256                     }
257                     $i++;
258                 }
259                 return $new_array;
260             } else {
261                 return null;
262             }
263         }
264     }
265
266
267     // ==================================================================
268     // Function to get column meta data info pertaining to the last query
269     // see docs for more info and usage
270
271     function get_col_info($info_type = 'name', $col_offset = -1) {
272         if ( $this->col_info ) {
273             if ( $col_offset == -1 ) {
274                 $i = 0;
275                 foreach($this->col_info as $col ) {
276                     $new_array[$i] = $col->{$info_type};
277                     $i++;
278                 }
279                 return $new_array;
280             } else {
281                 return $this->col_info[$col_offset]->{$info_type};
282             }
283         }
284     }
285
286     function timer_start() {
287         $mtime = microtime();
288         $mtime = explode(' ', $mtime);
289         $this->time_start = $mtime[1] + $mtime[0];
290         return true;
291     }
292     
293     function timer_stop($precision = 3) {
294         $mtime = microtime();
295         $mtime = explode(' ', $mtime);
296         $time_end = $mtime[1] + $mtime[0];
297         $time_total = $time_end - $this->time_start;
298         return $time_total;
299     }
300
301     function bail($message) { // Just wraps errors in a nice header and footer
302     if ( !$this->show_errors )
303         return false;
304     header( 'Content-Type: text/html; charset=utf-8');       
305     echo <<<HEAD
306     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
307     <html xmlns="http://www.w3.org/1999/xhtml">
308     <head>
309         <title>WordPress &rsaquo; Error</title>
310         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
311         <style media="screen" type="text/css">
312         <!--
313         html {
314             background: #eee;
315         }
316         body {
317             background: #fff;
318             color: #000;
319             font-family: Georgia, "Times New Roman", Times, serif;
320             margin-left: 25%;
321             margin-right: 25%;
322             padding: .2em 2em;
323         }
324         
325         h1 {
326             color: #006;
327             font-size: 18px;
328             font-weight: lighter;
329         }
330         
331         h2 {
332             font-size: 16px;
333         }
334         
335         p, li, dt {
336             line-height: 140%;
337             padding-bottom: 2px;
338         }
339     
340         ul, ol {
341             padding: 5px 5px 5px 20px;
342         }
343         #logo {
344             margin-bottom: 2em;
345         }
346         -->
347         </style>
348     </head>
349     <body>
350     <h1 id="logo"><img alt="WordPress" src="http://static.wordpress.org/logo.png" /></h1>
351 HEAD;
352     echo $message;
353     echo "</body></html>";
354     die();
355     }
356 }
357
358 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
359 ?>
Note: See TracBrowser for help on using the browser.