Changeset 1180
- Timestamp:
- 04/26/2004 02:54:06 AM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/wp-db.php
r1178 r1180 1 1 <?php 2 // ================================================================== 3 // Author: Justin Vincent (justin@visunet.ie) 4 // Web: http://php.justinvincent.com 5 // Name: ezSQL 6 // Desc: Class to make it very easy to deal with mySQL database connections. 7 // WordPress is using this class to make the code cleaner and faster. 8 // We highly recommend it. 9 // We have modified the HTML it returns slightly. 10 11 define('EZSQL_VERSION', '1.21'); 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'); 12 9 define('OBJECT', 'OBJECT', true); 13 define('ARRAY_A', 'ARRAY_A', true); 14 define('ARRAY_N', 'ARRAY_N', true); 15 define('SAVEQUERIES', true); 16 17 // The Main Class, renamed to avoid conflicts. 10 define('ARRAY_A', 'ARRAY_A', false); 11 define('ARRAY_N', 'ARRAY_N', false); 12 if (!defined('SAVEQUERIES')) 13 define('SAVEQUERIES', false); 18 14 19 15 class wpdb { 20 16 21 var $debug_called;22 var $vardump_called;23 17 var $show_errors = true; 24 var $querycount; 18 var $num_queries = 0; 19 var $last_query; 20 var $col_info; 25 21 26 22 // ================================================================== … … 29 25 function wpdb($dbuser, $dbpassword, $dbname, $dbhost) { 30 26 $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword); 31 if ( ! $this->dbh ){27 if (!$this->dbh) { 32 28 die("<div> 33 29 <p><strong>Error establishing a database connection!</strong> This probably means that the connection information in your <code>wp-config.php</code> file is incorrect. Double check it and try again.</p> … … 42 38 43 39 $this->select($dbname); 44 $this->querycount = 0;45 40 } 46 41 … … 49 44 50 45 function select($db) { 51 if ( 46 if (!@mysql_select_db($db,$this->dbh)) { 52 47 die(" 53 48 <p>We're having a little trouble selecting the proper database for WordPress.</p> … … 72 67 73 68 function print_error($str = '') { 74 // All errors go to the global error array $EZSQL_ERROR..75 69 global $EZSQL_ERROR; 76 77 // If no special error string then use mysql default.. 78 if ( !$str ) $str = mysql_error(); 79 80 // Log this error to the global array.. 81 $EZSQL_ERROR[] = array 82 ( 83 'query' => $this->last_query, 84 'error_str' => $str 85 ); 70 if (!$str) $str = mysql_error(); 71 $EZSQL_ERROR[] = 72 array ('query' => $this->last_query, 'error_str' => $str); 86 73 87 74 // Is error output turned on or not.. … … 89 76 // If there is an error then take note of it 90 77 print "<div id='error'> 91 <p><strong>SQL/DB Error:</strong><br /> 92 [<span style='color: #007;'>$str</span>]<br /> 78 <p><strong>Database error:</strong> [$str]<br /> 93 79 <code>$this->last_query</code></p> 94 80 </div>"; … … 96 82 return false; 97 83 } 98 99 84 } 100 85 … … 114 99 115 100 function flush() { 116 // Get rid of these117 101 $this->last_result = null; 118 102 $this->col_info = null; 119 103 $this->last_query = null; 120 121 104 } 122 105 … … 125 108 126 109 function query($query) { 127 128 // Flush cached values..110 // initialise return 111 $return_val = 0; 129 112 $this->flush(); 130 113 … … 136 119 137 120 // Perform the query via std mysql_query function.. 138 $this->result = mysql_query($query,$this->dbh);139 ++$this-> querycount;121 $this->result = @mysql_query($query,$this->dbh); 122 ++$this->num_queries; 140 123 if (SAVEQUERIES) { 141 124 $this->savedqueries[] = $query; 142 125 } 143 126 144 // If there was an insert, delete or update see how many rows were affected 145 // (Also, If there there was an insert take note of the insert_id 146 $query_type = array('insert','delete','update','replace'); 147 148 // loop through the above array 149 foreach ( $query_type as $word ) { 150 // This is true if the query starts with insert, delete or update 151 if ( preg_match("/^\\s*$word /i",$query) ) { 152 $this->rows_affected = mysql_affected_rows(); 153 // This gets the insert ID 154 if ( $word == 'insert' || $word == 'replace' ) { 155 $this->insert_id = mysql_insert_id($this->dbh); 156 } 157 $this->result = false; 158 } 159 } 160 161 if ( mysql_error() ) { // If there is an error then take note of it.. 127 // If there is an error then take note of it.. 128 if ( mysql_error() ) { 162 129 $this->print_error(); 130 return false; 131 } 132 133 if ( preg_match("/^\\s*(insert|delete|update|replace) /i",$query) ) { 134 $this->rows_affected = mysql_affected_rows(); 135 // Take note of the insert_id 136 if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { 137 $this->insert_id = mysql_insert_id($this->dbh); 138 } 139 // Return number of rows affected 140 $return_val = $this->rows_affected; 163 141 } else { 164 // In other words if this was a select statement.. 165 if ( $this->result ) { 166 // ======================================================= 167 // Take note of column info 168 169 $i=0; 170 while ($i < @mysql_num_fields($this->result)) { 171 $this->col_info[$i] = @mysql_fetch_field($this->result); 172 $i++; 173 } 174 175 // ======================================================= 176 // Store Query Results 177 178 $i=0; 179 while ( $row = @mysql_fetch_object($this->result) ) { 180 // Store relults as an objects within main array 181 $this->last_result[$i] = $row; 182 $i++; 183 } 184 185 // Log number of rows the query returned 186 $this->num_rows = $i; 187 188 @mysql_free_result($this->result); 189 190 191 // If there were results then return true for $db->query 192 if ( $i ) { 193 return true; 194 } else { 195 return false; 196 } 197 198 } else { // Update insert etc. was good.. 199 return true; 200 } 201 } 142 $i = 0; 143 while ($i < @mysql_num_fields($this->result)) { 144 $this->col_info[$i] = @mysql_fetch_field($this->result); 145 $i++; 146 } 147 $num_rows = 0; 148 while ( $row = @mysql_fetch_object($this->result) ) { 149 $this->last_result[$num_rows] = $row; 150 $num_rows++; 151 } 152 153 @mysql_free_result($this->result); 154 155 // Log number of rows the query returned 156 $this->num_rows = $num_rows; 157 158 // Return number of rows selected 159 $return_val = $this->num_rows; 160 } 161 162 // If debug ALL queries 163 $this->trace || $this->debug_all ? $this->debug() : null ; 164 165 return $return_val; 202 166 } 203 167 … … 205 169 // Get one variable from the DB - see docs for more detail 206 170 207 function get_var($query=null, $x=0, $y=0) { 208 209 // Log how the function was called 171 function get_var($query=null, $x = 0, $y = 0) { 210 172 $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; 211 212 // If there is a query then perform it if not then use cached results.. 213 if ( $query ) { 214 $this->query($query); 215 } 173 if ( $query ) 174 $this->query($query); 216 175 217 176 // Extract var out of cached results based x,y vals … … 227 186 // Get one row from the DB - see docs for more detail 228 187 229 function get_row($query=null, $output=OBJECT, $y=0) { 230 231 // Log how the function was called 188 function get_row($query = null, $output = OBJECT, $y = 0) { 232 189 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; 233 234 // If there is a query then perform it if not then use cached results.. 235 if ( $query ) { 236 $this->query($query); 237 } 238 239 // If the output is an object then return object using the row offset.. 190 if ( $query ) 191 $this->query($query); 192 240 193 if ( $output == OBJECT ) { 241 return $this->last_result[$y]?$this->last_result[$y]:null; 242 } 243 // If the output is an associative array then return row as such.. 244 elseif ( $output == ARRAY_A ) { 245 return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null; 246 } 247 // If the output is an numerical array then return row as such.. 248 elseif ( $output == ARRAY_N ) { 249 return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null; 250 } 251 // If invalid output type was specified.. 252 else { 194 return $this->last_result[$y] ? $this->last_result[$y] : null; 195 } elseif ( $output == ARRAY_A ) { 196 return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; 197 } elseif ( $output == ARRAY_N ) { 198 return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; 199 } else { 253 200 $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"); 254 201 } 255 256 202 } 257 203 … … 260 206 // se docs for usage and info 261 207 262 function get_col($query=null,$x=0) { 263 // If there is a query then perform it if not then use cached results.. 264 if ( $query ) { 265 $this->query($query); 266 } 208 function get_col($query = null , $x = 0) { 209 if ( $query ) 210 $this->query($query); 211 267 212 // Extract the column values 268 213 for ( $i=0; $i < count($this->last_result); $i++ ) { 269 $new_array[$i] = $this->get_var(null, $x,$i);214 $new_array[$i] = $this->get_var(null, $x, $i); 270 215 } 271 216 return $new_array; … … 275 220 // Return the the query as a result set - see docs for more details 276 221 277 function get_results($query=null, $output = OBJECT){ 278 279 // Log how the function was called 222 function get_results($query = null, $output = OBJECT) { 280 223 $this->func_call = "\$db->get_results(\"$query\", $output)"; 281 224 282 // If there is a query then perform it if not then use cached results.. 283 if ( $query ) { 284 $this->query($query); 285 } 225 if ( $query ) 226 $this->query($query); 286 227 287 228 // Send back array of objects. Each row is an object … … 290 231 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { 291 232 if ( $this->last_result ) { 292 $i =0;233 $i = 0; 293 234 foreach( $this->last_result as $row ) { 294 235 $new_array[$i] = (array) $row; … … 296 237 $new_array[$i] = array_values($new_array[$i]); 297 238 } 298 299 239 $i++; 300 240 } 301 302 241 return $new_array; 303 242 } else { … … 312 251 // see docs for more info and usage 313 252 314 function get_col_info($info_type='name', $col_offset=-1) { 315 253 function get_col_info($info_type = 'name', $col_offset = -1) { 316 254 if ( $this->col_info ) { 317 255 if ( $col_offset == -1 ) { 318 $i =0;256 $i = 0; 319 257 foreach($this->col_info as $col ) { 320 258 $new_array[$i] = $col->{$info_type}; … … 325 263 return $this->col_info[$col_offset]->{$info_type}; 326 264 } 327 328 } 329 330 } 331 265 } 266 } 332 267 } 333 268
Note: See TracChangeset
for help on using the changeset viewer.