-
+ C50D1CDC8BEF79CF39337F95B452242B04A002ADB932A64510C4D58AF36E88A3243A58EEA6BCCA27161210F32C7C1ECF6B93D762A82E9091A6D00CD03F2DDA08
mp-wp/wp-includes/wp-db.php
(0 . 0)(1 . 996)
158135 <?php
158136
158137
158138 /**
158139 * WordPress DB Class
158140 *
158141 * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
158142 *
158143 * @package WordPress
158144 * @subpackage Database
158145 * @since 0.71
158146 */
158147
158148 /**
158149 * @since 0.71
158150 */
158151 define('EZSQL_VERSION', 'WP1.25');
158152
158153 /**
158154 * @since 0.71
158155 */
158156 define('OBJECT', 'OBJECT', true);
158157
158158 /**
158159 * @since {@internal Version Unknown}}
158160 */
158161 define('OBJECT_K', 'OBJECT_K', false);
158162
158163 /**
158164 * @since 0.71
158165 */
158166 define('ARRAY_A', 'ARRAY_A', false);
158167
158168 /**
158169 * @since 0.71
158170 */
158171 define('ARRAY_N', 'ARRAY_N', false);
158172
158173 /**
158174 * WordPress Database Access Abstraction Object
158175 *
158176 * It is possible to replace this class with your own
158177 * by setting the $wpdb global variable in wp-content/db.php
158178 * file with your class. You can name it wpdb also, since
158179 * this file will not be included, if the other file is
158180 * available.
158181 *
158182 * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
158183 *
158184 * @package WordPress
158185 * @subpackage Database
158186 * @since 0.71
158187 * @final
158188 */
158189 class wpdb {
158190
158191 /**
158192 * Whether to show SQL/DB errors
158193 *
158194 * @since 0.71
158195 * @access private
158196 * @var bool
158197 */
158198 var $show_errors = false;
158199
158200 /**
158201 * Whether to suppress errors during the DB bootstrapping.
158202 *
158203 * @access private
158204 * @since {@internal Version Unknown}}
158205 * @var bool
158206 */
158207 var $suppress_errors = false;
158208
158209 /**
158210 * The last error during query.
158211 *
158212 * @since {@internal Version Unknown}}
158213 * @var string
158214 */
158215 var $last_error = '';
158216
158217 /**
158218 * Amount of queries made
158219 *
158220 * @since 1.2.0
158221 * @access private
158222 * @var int
158223 */
158224 var $num_queries = 0;
158225
158226 /**
158227 * Saved result of the last query made
158228 *
158229 * @since 1.2.0
158230 * @access private
158231 * @var array
158232 */
158233 var $last_query;
158234
158235 /**
158236 * Saved info on the table column
158237 *
158238 * @since 1.2.0
158239 * @access private
158240 * @var array
158241 */
158242 var $col_info;
158243
158244 /**
158245 * Saved queries that were executed
158246 *
158247 * @since 1.5.0
158248 * @access private
158249 * @var array
158250 */
158251 var $queries;
158252
158253 /**
158254 * WordPress table prefix
158255 *
158256 * You can set this to have multiple WordPress installations
158257 * in a single database. The second reason is for possible
158258 * security precautions.
158259 *
158260 * @since 0.71
158261 * @access private
158262 * @var string
158263 */
158264 var $prefix = '';
158265
158266 /**
158267 * Whether the database queries are ready to start executing.
158268 *
158269 * @since 2.5.0
158270 * @access private
158271 * @var bool
158272 */
158273 var $ready = false;
158274
158275 /**
158276 * WordPress Posts table
158277 *
158278 * @since 1.5.0
158279 * @access public
158280 * @var string
158281 */
158282 var $posts;
158283
158284 /**
158285 * WordPress Users table
158286 *
158287 * @since 1.5.0
158288 * @access public
158289 * @var string
158290 */
158291 var $users;
158292
158293 /**
158294 * WordPress Categories table
158295 *
158296 * @since 1.5.0
158297 * @access public
158298 * @var string
158299 */
158300 var $categories;
158301
158302 /**
158303 * WordPress Post to Category table
158304 *
158305 * @since 1.5.0
158306 * @access public
158307 * @var string
158308 */
158309 var $post2cat;
158310
158311 /**
158312 * WordPress Comments table
158313 *
158314 * @since 1.5.0
158315 * @access public
158316 * @var string
158317 */
158318 var $comments;
158319
158320 /**
158321 * WordPress Links table
158322 *
158323 * @since 1.5.0
158324 * @access public
158325 * @var string
158326 */
158327 var $links;
158328
158329 /**
158330 * WordPress Options table
158331 *
158332 * @since 1.5.0
158333 * @access public
158334 * @var string
158335 */
158336 var $options;
158337
158338 /**
158339 * WordPress Post Metadata table
158340 *
158341 * @since {@internal Version Unknown}}
158342 * @access public
158343 * @var string
158344 */
158345 var $postmeta;
158346
158347 /**
158348 * WordPress User Metadata table
158349 *
158350 * @since 2.3.0
158351 * @access public
158352 * @var string
158353 */
158354 var $usermeta;
158355
158356 /**
158357 * WordPress Terms table
158358 *
158359 * @since 2.3.0
158360 * @access public
158361 * @var string
158362 */
158363 var $terms;
158364
158365 /**
158366 * WordPress Term Taxonomy table
158367 *
158368 * @since 2.3.0
158369 * @access public
158370 * @var string
158371 */
158372 var $term_taxonomy;
158373
158374 /**
158375 * WordPress Term Relationships table
158376 *
158377 * @since 2.3.0
158378 * @access public
158379 * @var string
158380 */
158381 var $term_relationships;
158382
158383 /**
158384 * List of WordPress tables
158385 *
158386 * @since {@internal Version Unknown}}
158387 * @access private
158388 * @var array
158389 */
158390 var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
158391 'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
158392
158393 /**
158394 * Database table columns charset
158395 *
158396 * @since 2.2.0
158397 * @access public
158398 * @var string
158399 */
158400 var $charset;
158401
158402 /**
158403 * Database table columns collate
158404 *
158405 * @since 2.2.0
158406 * @access public
158407 * @var string
158408 */
158409 var $collate;
158410
158411 /**
158412 * Connects to the database server and selects a database
158413 *
158414 * PHP4 compatibility layer for calling the PHP5 constructor.
158415 *
158416 * @uses wpdb::__construct() Passes parameters and returns result
158417 * @since 0.71
158418 *
158419 * @param string $dbuser MySQL database user
158420 * @param string $dbpassword MySQL database password
158421 * @param string $dbname MySQL database name
158422 * @param string $dbhost MySQL database host
158423 */
158424 function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
158425 return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
158426 }
158427
158428 /**
158429 * Connects to the database server and selects a database
158430 *
158431 * PHP5 style constructor for compatibility with PHP5. Does
158432 * the actual setting up of the class properties and connection
158433 * to the database.
158434 *
158435 * @since 2.0.8
158436 *
158437 * @param string $dbuser MySQL database user
158438 * @param string $dbpassword MySQL database password
158439 * @param string $dbname MySQL database name
158440 * @param string $dbhost MySQL database host
158441 */
158442 function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
158443 register_shutdown_function(array(&$this, "__destruct"));
158444
158445 if ( defined('WP_DEBUG') and WP_DEBUG == true )
158446 $this->show_errors();
158447
158448 if ( defined('DB_CHARSET') )
158449 $this->charset = DB_CHARSET;
158450
158451 if ( defined('DB_COLLATE') )
158452 $this->collate = DB_COLLATE;
158453
158454 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
158455 if (!$this->dbh) {
158456 $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/"
158457 <h1>Error establishing a database connection</h1>
158458 <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>%s</code>. This could mean your host's database server is down.</p>
158459 <ul>
158460 <li>Are you sure you have the correct username and password?</li>
158461 <li>Are you sure that you have typed the correct hostname?</li>
158462 <li>Are you sure that the database server is running?</li>
158463 </ul>
158464 <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>
158465 "/*/WP_I18N_DB_CONN_ERROR*/, $dbhost));
158466 return;
158467 }
158468
158469 $this->ready = true;
158470
158471 if ( $this->has_cap( 'collation' ) ) {
158472 $collation_query = '';
158473 if ( !empty($this->charset) ) {
158474 $collation_query = "SET NAMES '{$this->charset}'";
158475 if (!empty($this->collate) )
158476 $collation_query .= " COLLATE '{$this->collate}'";
158477 }
158478
158479 if ( !empty($collation_query) )
158480 $this->query($collation_query);
158481
158482 }
158483
158484 $this->select($dbname);
158485 }
158486
158487 /**
158488 * PHP5 style destructor and will run when database object is destroyed.
158489 *
158490 * @since 2.0.8
158491 *
158492 * @return bool Always true
158493 */
158494 function __destruct() {
158495 return true;
158496 }
158497
158498 /**
158499 * Sets the table prefix for the WordPress tables.
158500 *
158501 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
158502 * override the WordPress users and usersmeta tables.
158503 *
158504 * @since 2.5.0
158505 *
158506 * @param string $prefix Alphanumeric name for the new prefix.
158507 * @return string Old prefix
158508 */
158509 function set_prefix($prefix) {
158510
158511 if ( preg_match('|[^a-z0-9_]|i', $prefix) )
158512 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
158513
158514 $old_prefix = $this->prefix;
158515 $this->prefix = $prefix;
158516
158517 foreach ( (array) $this->tables as $table )
158518 $this->$table = $this->prefix . $table;
158519
158520 if ( defined('CUSTOM_USER_TABLE') )
158521 $this->users = CUSTOM_USER_TABLE;
158522
158523 if ( defined('CUSTOM_USER_META_TABLE') )
158524 $this->usermeta = CUSTOM_USER_META_TABLE;
158525
158526 return $old_prefix;
158527 }
158528
158529 /**
158530 * Selects a database using the current database connection.
158531 *
158532 * The database name will be changed based on the current database
158533 * connection. On failure, the execution will bail and display an DB error.
158534 *
158535 * @since 0.71
158536 *
158537 * @param string $db MySQL database name
158538 * @return null Always null.
158539 */
158540 function select($db) {
158541 if (!@mysql_select_db($db, $this->dbh)) {
158542 $this->ready = false;
158543 $this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
158544 <h1>Can’t select database</h1>
158545 <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>%1$s</code> database.</p>
158546 <ul>
158547 <li>Are you sure it exists?</li>
158548 <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
158549 <li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li>
158550 </ul>
158551 <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>'/*/WP_I18N_DB_SELECT_DB*/, $db, DB_USER));
158552 return;
158553 }
158554 }
158555
158556 /**
158557 * Escapes content for insertion into the database, for security
158558 *
158559 * @since 0.71
158560 *
158561 * @param string $string
158562 * @return string query safe string
158563 */
158564 function escape($string) {
158565 return addslashes( $string );
158566 // Disable rest for now, causing problems
158567 /*
158568 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )
158569 return mysql_escape_string( $string );
158570 else
158571 return mysql_real_escape_string( $string, $this->dbh );
158572 */
158573 }
158574
158575 /**
158576 * Escapes content by reference for insertion into the database, for security
158577 *
158578 * @since 2.3.0
158579 *
158580 * @param string $s
158581 */
158582 function escape_by_ref(&$s) {
158583 $s = $this->escape($s);
158584 }
158585
158586 /**
158587 * Prepares a SQL query for safe use, using sprintf() syntax.
158588 *
158589 * @link http://php.net/sprintf See for syntax to use for query string.
158590 * @since 2.3.0
158591 *
158592 * @param null|string $args If string, first parameter must be query statement
158593 * @param mixed $args,... If additional parameters, they will be set inserted into the query.
158594 * @return null|string Sanitized query string
158595 */
158596 function prepare($args=null) {
158597 if ( is_null( $args ) )
158598 return;
158599 $args = func_get_args();
158600 $query = array_shift($args);
158601 $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
158602 $query = str_replace('"%s"', '%s', $query); // doublequote unquoting
158603 $query = str_replace('%s', "'%s'", $query); // quote the strings
158604 array_walk($args, array(&$this, 'escape_by_ref'));
158605 return @vsprintf($query, $args);
158606 }
158607
158608 /**
158609 * Print SQL/DB error.
158610 *
158611 * @since 0.71
158612 * @global array $EZSQL_ERROR Stores error information of query and error string
158613 *
158614 * @param string $str The error to display
158615 * @return bool False if the showing of errors is disabled.
158616 */
158617 function print_error($str = '') {
158618 global $EZSQL_ERROR;
158619
158620 if (!$str) $str = mysql_error($this->dbh);
158621 $EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
158622
158623 if ( $this->suppress_errors )
158624 return false;
158625
158626 if ( $caller = $this->get_caller() )
158627 $error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR_FULL*/'WordPress database error %1$s for query %2$s made by %3$s'/*/WP_I18N_DB_QUERY_ERROR_FULL*/, $str, $this->last_query, $caller);
158628 else
158629 $error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR*/'WordPress database error %1$s for query %2$s'/*/WP_I18N_DB_QUERY_ERROR*/, $str, $this->last_query);
158630
158631 $log_error = true;
158632 if ( ! function_exists('error_log') )
158633 $log_error = false;
158634
158635 $log_file = @ini_get('error_log');
158636 if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )
158637 $log_error = false;
158638
158639 if ( $log_error )
158640 @error_log($error_str, 0);
158641
158642 // Is error output turned on or not..
158643 if ( !$this->show_errors )
158644 return false;
158645
158646 $str = htmlspecialchars($str, ENT_QUOTES);
158647 $query = htmlspecialchars($this->last_query, ENT_QUOTES);
158648
158649 // If there is an error then take note of it
158650 print "<div id='error'>
158651 <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
158652 <code>$query</code></p>
158653 </div>";
158654 }
158655
158656 /**
158657 * Enables showing of database errors.
158658 *
158659 * This function should be used only to enable showing of errors.
158660 * wpdb::hide_errors() should be used instead for hiding of errors. However,
158661 * this function can be used to enable and disable showing of database
158662 * errors.
158663 *
158664 * @since 0.71
158665 *
158666 * @param bool $show Whether to show or hide errors
158667 * @return bool Old value for showing errors.
158668 */
158669 function show_errors( $show = true ) {
158670 $errors = $this->show_errors;
158671 $this->show_errors = $show;
158672 return $errors;
158673 }
158674
158675 /**
158676 * Disables showing of database errors.
158677 *
158678 * @since 0.71
158679 *
158680 * @return bool Whether showing of errors was active or not
158681 */
158682 function hide_errors() {
158683 $show = $this->show_errors;
158684 $this->show_errors = false;
158685 return $show;
158686 }
158687
158688 /**
158689 * Whether to suppress database errors.
158690 *
158691 * @param unknown_type $suppress
158692 * @return unknown
158693 */
158694 function suppress_errors( $suppress = true ) {
158695 $errors = $this->suppress_errors;
158696 $this->suppress_errors = $suppress;
158697 return $errors;
158698 }
158699
158700 /**
158701 * Kill cached query results.
158702 *
158703 * @since 0.71
158704 */
158705 function flush() {
158706 $this->last_result = array();
158707 $this->col_info = null;
158708 $this->last_query = null;
158709 }
158710
158711 /**
158712 * Perform a MySQL database query, using current database connection.
158713 *
158714 * More information can be found on the codex page.
158715 *
158716 * @since 0.71
158717 *
158718 * @param string $query
158719 * @return unknown
158720 */
158721 function query($query) {
158722 if ( ! $this->ready )
158723 return false;
158724
158725 // filter the query, if filters are available
158726 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
158727 if ( function_exists('apply_filters') )
158728 $query = apply_filters('query', $query);
158729
158730 // initialise return
158731 $return_val = 0;
158732 $this->flush();
158733
158734 // Log how the function was called
158735 $this->func_call = "\$db->query(\"$query\")";
158736
158737 // Keep track of the last query for debug..
158738 $this->last_query = $query;
158739
158740 // Perform the query via std mysql_query function..
158741 if ( defined('SAVEQUERIES') && SAVEQUERIES )
158742 $this->timer_start();
158743
158744 $this->result = @mysql_query($query, $this->dbh);
158745 ++$this->num_queries;
158746
158747 if ( defined('SAVEQUERIES') && SAVEQUERIES )
158748 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
158749
158750 // If there is an error then take note of it..
158751 if ( $this->last_error = mysql_error($this->dbh) ) {
158752 $this->print_error();
158753 return false;
158754 }
158755
158756 if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
158757 $this->rows_affected = mysql_affected_rows($this->dbh);
158758 // Take note of the insert_id
158759 if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
158760 $this->insert_id = mysql_insert_id($this->dbh);
158761 }
158762 // Return number of rows affected
158763 $return_val = $this->rows_affected;
158764 } else {
158765 $i = 0;
158766 while ($i < @mysql_num_fields($this->result)) {
158767 $this->col_info[$i] = @mysql_fetch_field($this->result);
158768 $i++;
158769 }
158770 $num_rows = 0;
158771 while ( $row = @mysql_fetch_object($this->result) ) {
158772 $this->last_result[$num_rows] = $row;
158773 $num_rows++;
158774 }
158775
158776 @mysql_free_result($this->result);
158777
158778 // Log number of rows the query returned
158779 $this->num_rows = $num_rows;
158780
158781 // Return number of rows selected
158782 $return_val = $this->num_rows;
158783 }
158784
158785 return $return_val;
158786 }
158787
158788 /**
158789 * Insert an array of data into a table.
158790 *
158791 * @since 2.5.0
158792 *
158793 * @param string $table WARNING: not sanitized!
158794 * @param array $data Should not already be SQL-escaped
158795 * @return mixed Results of $this->query()
158796 */
158797 function insert($table, $data) {
158798 $data = add_magic_quotes($data);
158799 $fields = array_keys($data);
158800 return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')");
158801 }
158802
158803 /**
158804 * Update a row in the table with an array of data.
158805 *
158806 * @since 2.5.0
158807 *
158808 * @param string $table WARNING: not sanitized!
158809 * @param array $data Should not already be SQL-escaped
158810 * @param array $where A named array of WHERE column => value relationships. Multiple member pairs will be joined with ANDs. WARNING: the column names are not currently sanitized!
158811 * @return mixed Results of $this->query()
158812 */
158813 function update($table, $data, $where){
158814 $data = add_magic_quotes($data);
158815 $bits = $wheres = array();
158816 foreach ( (array) array_keys($data) as $k )
158817 $bits[] = "`$k` = '$data[$k]'";
158818
158819 if ( is_array( $where ) )
158820 foreach ( $where as $c => $v )
158821 $wheres[] = "$c = '" . $this->escape( $v ) . "'";
158822 else
158823 return false;
158824
158825 return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) );
158826 }
158827
158828 /**
158829 * Retrieve one variable from the database.
158830 *
158831 * This combines the functionality of wpdb::get_row() and wpdb::get_col(),
158832 * so both the column and row can be picked.
158833 *
158834 * It is possible to use this function without executing more queries. If
158835 * you already made a query, you can set the $query to 'null' value and just
158836 * retrieve either the column and row of the last query result.
158837 *
158838 * @since 0.71
158839 *
158840 * @param string $query Can be null as well, for caching
158841 * @param int $x Column num to return
158842 * @param int $y Row num to return
158843 * @return mixed Database query results
158844 */
158845 function get_var($query=null, $x = 0, $y = 0) {
158846 $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
158847 if ( $query )
158848 $this->query($query);
158849
158850 // Extract var out of cached results based x,y vals
158851 if ( !empty( $this->last_result[$y] ) ) {
158852 $values = array_values(get_object_vars($this->last_result[$y]));
158853 }
158854
158855 // If there is a value return it else return null
158856 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
158857 }
158858
158859 /**
158860 * Retrieve one row from the database.
158861 *
158862 * @since 0.71
158863 *
158864 * @param string $query SQL query
158865 * @param string $output ARRAY_A | ARRAY_N | OBJECT
158866 * @param int $y Row num to return
158867 * @return mixed Database query results
158868 */
158869 function get_row($query = null, $output = OBJECT, $y = 0) {
158870 $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
158871 if ( $query )
158872 $this->query($query);
158873 else
158874 return null;
158875
158876 if ( !isset($this->last_result[$y]) )
158877 return null;
158878
158879 if ( $output == OBJECT ) {
158880 return $this->last_result[$y] ? $this->last_result[$y] : null;
158881 } elseif ( $output == ARRAY_A ) {
158882 return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
158883 } elseif ( $output == ARRAY_N ) {
158884 return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
158885 } else {
158886 $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
158887 }
158888 }
158889
158890 /**
158891 * Retrieve one column from the database.
158892 *
158893 * @since 0.71
158894 *
158895 * @param string $query Can be null as well, for caching
158896 * @param int $x Col num to return. Starts from 0.
158897 * @return array Column results
158898 */
158899 function get_col($query = null , $x = 0) {
158900 if ( $query )
158901 $this->query($query);
158902
158903 $new_array = array();
158904 // Extract the column values
158905 for ( $i=0; $i < count($this->last_result); $i++ ) {
158906 $new_array[$i] = $this->get_var(null, $x, $i);
158907 }
158908 return $new_array;
158909 }
158910
158911 /**
158912 * Retrieve an entire result set from the database.
158913 *
158914 * @since 0.71
158915 *
158916 * @param string|null $query Can also be null to pull from the cache
158917 * @param string $output ARRAY_A | ARRAY_N | OBJECT_K | OBJECT
158918 * @return mixed Database query results
158919 */
158920 function get_results($query = null, $output = OBJECT) {
158921 $this->func_call = "\$db->get_results(\"$query\", $output)";
158922
158923 if ( $query )
158924 $this->query($query);
158925 else
158926 return null;
158927
158928 if ( $output == OBJECT ) {
158929 // Return an integer-keyed array of row objects
158930 return $this->last_result;
158931 } elseif ( $output == OBJECT_K ) {
158932 // Return an array of row objects with keys from column 1
158933 // (Duplicates are discarded)
158934 foreach ( $this->last_result as $row ) {
158935 $key = array_shift( get_object_vars( $row ) );
158936 if ( !isset( $new_array[ $key ] ) )
158937 $new_array[ $key ] = $row;
158938 }
158939 return $new_array;
158940 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
158941 // Return an integer-keyed array of...
158942 if ( $this->last_result ) {
158943 $i = 0;
158944 foreach( (array) $this->last_result as $row ) {
158945 if ( $output == ARRAY_N ) {
158946 // ...integer-keyed row arrays
158947 $new_array[$i] = array_values( get_object_vars( $row ) );
158948 } else {
158949 // ...column name-keyed row arrays
158950 $new_array[$i] = get_object_vars( $row );
158951 }
158952 ++$i;
158953 }
158954 return $new_array;
158955 }
158956 }
158957 }
158958
158959 /**
158960 * Retrieve column metadata from the last query.
158961 *
158962 * @since 0.71
158963 *
158964 * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
158965 * @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
158966 * @return mixed Column Results
158967 */
158968 function get_col_info($info_type = 'name', $col_offset = -1) {
158969 if ( $this->col_info ) {
158970 if ( $col_offset == -1 ) {
158971 $i = 0;
158972 foreach( (array) $this->col_info as $col ) {
158973 $new_array[$i] = $col->{$info_type};
158974 $i++;
158975 }
158976 return $new_array;
158977 } else {
158978 return $this->col_info[$col_offset]->{$info_type};
158979 }
158980 }
158981 }
158982
158983 /**
158984 * Starts the timer, for debugging purposes.
158985 *
158986 * @since 1.5.0
158987 *
158988 * @return bool Always returns true
158989 */
158990 function timer_start() {
158991 $mtime = microtime();
158992 $mtime = explode(' ', $mtime);
158993 $this->time_start = $mtime[1] + $mtime[0];
158994 return true;
158995 }
158996
158997 /**
158998 * Stops the debugging timer.
158999 *
159000 * @since 1.5.0
159001 *
159002 * @return int Total time spent on the query, in milliseconds
159003 */
159004 function timer_stop() {
159005 $mtime = microtime();
159006 $mtime = explode(' ', $mtime);
159007 $time_end = $mtime[1] + $mtime[0];
159008 $time_total = $time_end - $this->time_start;
159009 return $time_total;
159010 }
159011
159012 /**
159013 * Wraps fatal errors in a nice header and footer and dies.
159014 *
159015 * @since 1.5.0
159016 *
159017 * @param string $message
159018 * @return unknown
159019 */
159020 function bail($message) {
159021 if ( !$this->show_errors ) {
159022 if ( class_exists('WP_Error') )
159023 $this->error = new WP_Error('500', $message);
159024 else
159025 $this->error = $message;
159026 return false;
159027 }
159028 wp_die($message);
159029 }
159030
159031 /**
159032 * Whether or not MySQL database is minimal required version.
159033 *
159034 * @since 2.5.0
159035 * @uses $wp_version
159036 *
159037 * @return WP_Error
159038 */
159039 function check_database_version()
159040 {
159041 global $wp_version;
159042 // Make sure the server has MySQL 4.0
159043 if ( version_compare($this->db_version(), '4.0.0', '<') )
159044 return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $wp_version));
159045 }
159046
159047 /**
159048 * Whether of not the database version supports collation.
159049 *
159050 * Called when WordPress is generating the table scheme.
159051 *
159052 * @since 2.5.0
159053 *
159054 * @return bool True if collation is supported, false if version does not
159055 */
159056 function supports_collation()
159057 {
159058 return $this->has_cap( 'collation' );
159059 }
159060
159061 /**
159062 * Generic function to determine if a database supports a particular feature
159063 * @param string $db_cap the feature
159064 * @param false|string|resource $dbh_or_table the databaese (the current database, the database housing the specified table, or the database of the mysql resource)
159065 * @return bool
159066 */
159067 function has_cap( $db_cap ) {
159068 $version = $this->db_version();
159069
159070 switch ( strtolower( $db_cap ) ) :
159071 case 'collation' : // @since 2.5.0
159072 case 'group_concat' : // @since 2.7
159073 case 'subqueries' : // @since 2.7
159074 return version_compare($version, '4.1', '>=');
159075 break;
159076 endswitch;
159077
159078 return false;
159079 }
159080
159081 /**
159082 * Retrieve the name of the function that called wpdb.
159083 *
159084 * Requires PHP 4.3 and searches up the list of functions until it reaches
159085 * the one that would most logically had called this method.
159086 *
159087 * @since 2.5.0
159088 *
159089 * @return string The name of the calling function
159090 */
159091 function get_caller() {
159092 // requires PHP 4.3+
159093 if ( !is_callable('debug_backtrace') )
159094 return '';
159095
159096 $bt = debug_backtrace();
159097 $caller = array();
159098
159099 $bt = array_reverse( $bt );
159100 foreach ( (array) $bt as $call ) {
159101 if ( @$call['class'] == __CLASS__ )
159102 continue;
159103 $function = $call['function'];
159104 if ( isset( $call['class'] ) )
159105 $function = $call['class'] . "->$function";
159106 $caller[] = $function;
159107 }
159108 $caller = join( ', ', $caller );
159109
159110 return $caller;
159111 }
159112
159113 /**
159114 * The database version number
159115 * @return false|string false on failure, version number on success
159116 */
159117 function db_version() {
159118 return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));
159119 }
159120 }
159121
159122 if ( ! isset($wpdb) ) {
159123 /**
159124 * WordPress Database Object, if it isn't set already in wp-content/db.php
159125 * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database
159126 * @since 0.71
159127 */
159128 $wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
159129 }
159130 ?>