web/wp-includes/wp-db.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * WordPress DB Class
       
     4  *
       
     5  * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
       
     6  *
       
     7  * @package WordPress
       
     8  * @subpackage Database
       
     9  * @since 0.71
       
    10  */
       
    11 
       
    12 /**
       
    13  * @since 0.71
       
    14  */
       
    15 define('EZSQL_VERSION', 'WP1.25');
       
    16 
       
    17 /**
       
    18  * @since 0.71
       
    19  */
       
    20 define('OBJECT', 'OBJECT', true);
       
    21 
       
    22 /**
       
    23  * @since {@internal Version Unknown}}
       
    24  */
       
    25 define('OBJECT_K', 'OBJECT_K', false);
       
    26 
       
    27 /**
       
    28  * @since 0.71
       
    29  */
       
    30 define('ARRAY_A', 'ARRAY_A', false);
       
    31 
       
    32 /**
       
    33  * @since 0.71
       
    34  */
       
    35 define('ARRAY_N', 'ARRAY_N', false);
       
    36 
       
    37 /**
       
    38  * WordPress Database Access Abstraction Object
       
    39  *
       
    40  * It is possible to replace this class with your own
       
    41  * by setting the $wpdb global variable in wp-content/db.php
       
    42  * file with your class. You can name it wpdb also, since
       
    43  * this file will not be included, if the other file is
       
    44  * available.
       
    45  *
       
    46  * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
       
    47  *
       
    48  * @package WordPress
       
    49  * @subpackage Database
       
    50  * @since 0.71
       
    51  * @final
       
    52  */
       
    53 class wpdb {
       
    54 
       
    55 	/**
       
    56 	 * Whether to show SQL/DB errors
       
    57 	 *
       
    58 	 * @since 0.71
       
    59 	 * @access private
       
    60 	 * @var bool
       
    61 	 */
       
    62 	var $show_errors = false;
       
    63 
       
    64 	/**
       
    65 	 * Whether to suppress errors during the DB bootstrapping.
       
    66 	 *
       
    67 	 * @access private
       
    68 	 * @since {@internal Version Unknown}}
       
    69 	 * @var bool
       
    70 	 */
       
    71 	var $suppress_errors = false;
       
    72 
       
    73 	/**
       
    74 	 * The last error during query.
       
    75 	 *
       
    76 	 * @since {@internal Version Unknown}}
       
    77 	 * @var string
       
    78 	 */
       
    79 	var $last_error = '';
       
    80 
       
    81 	/**
       
    82 	 * Amount of queries made
       
    83 	 *
       
    84 	 * @since 1.2.0
       
    85 	 * @access private
       
    86 	 * @var int
       
    87 	 */
       
    88 	var $num_queries = 0;
       
    89 
       
    90 	/**
       
    91 	 * Saved result of the last query made
       
    92 	 *
       
    93 	 * @since 1.2.0
       
    94 	 * @access private
       
    95 	 * @var array
       
    96 	 */
       
    97 	var $last_query;
       
    98 
       
    99 	/**
       
   100 	 * Saved info on the table column
       
   101 	 *
       
   102 	 * @since 1.2.0
       
   103 	 * @access private
       
   104 	 * @var array
       
   105 	 */
       
   106 	var $col_info;
       
   107 
       
   108 	/**
       
   109 	 * Saved queries that were executed
       
   110 	 *
       
   111 	 * @since 1.5.0
       
   112 	 * @access private
       
   113 	 * @var array
       
   114 	 */
       
   115 	var $queries;
       
   116 
       
   117 	/**
       
   118 	 * WordPress table prefix
       
   119 	 *
       
   120 	 * You can set this to have multiple WordPress installations
       
   121 	 * in a single database. The second reason is for possible
       
   122 	 * security precautions.
       
   123 	 *
       
   124 	 * @since 0.71
       
   125 	 * @access private
       
   126 	 * @var string
       
   127 	 */
       
   128 	var $prefix = '';
       
   129 
       
   130 	/**
       
   131 	 * Whether the database queries are ready to start executing.
       
   132 	 *
       
   133 	 * @since 2.5.0
       
   134 	 * @access private
       
   135 	 * @var bool
       
   136 	 */
       
   137 	var $ready = false;
       
   138 
       
   139 	/**
       
   140 	 * WordPress Posts table
       
   141 	 *
       
   142 	 * @since 1.5.0
       
   143 	 * @access public
       
   144 	 * @var string
       
   145 	 */
       
   146 	var $posts;
       
   147 
       
   148 	/**
       
   149 	 * WordPress Users table
       
   150 	 *
       
   151 	 * @since 1.5.0
       
   152 	 * @access public
       
   153 	 * @var string
       
   154 	 */
       
   155 	var $users;
       
   156 
       
   157 	/**
       
   158 	 * WordPress Categories table
       
   159 	 *
       
   160 	 * @since 1.5.0
       
   161 	 * @access public
       
   162 	 * @var string
       
   163 	 */
       
   164 	var $categories;
       
   165 
       
   166 	/**
       
   167 	 * WordPress Post to Category table
       
   168 	 *
       
   169 	 * @since 1.5.0
       
   170 	 * @access public
       
   171 	 * @var string
       
   172 	 */
       
   173 	var $post2cat;
       
   174 
       
   175 	/**
       
   176 	 * WordPress Comments table
       
   177 	 *
       
   178 	 * @since 1.5.0
       
   179 	 * @access public
       
   180 	 * @var string
       
   181 	 */
       
   182 	var $comments;
       
   183 
       
   184 	/**
       
   185 	 * WordPress Links table
       
   186 	 *
       
   187 	 * @since 1.5.0
       
   188 	 * @access public
       
   189 	 * @var string
       
   190 	 */
       
   191 	var $links;
       
   192 
       
   193 	/**
       
   194 	 * WordPress Options table
       
   195 	 *
       
   196 	 * @since 1.5.0
       
   197 	 * @access public
       
   198 	 * @var string
       
   199 	 */
       
   200 	var $options;
       
   201 
       
   202 	/**
       
   203 	 * WordPress Post Metadata table
       
   204 	 *
       
   205 	 * @since {@internal Version Unknown}}
       
   206 	 * @access public
       
   207 	 * @var string
       
   208 	 */
       
   209 	var $postmeta;
       
   210 
       
   211 	/**
       
   212 	 * WordPress User Metadata table
       
   213 	 *
       
   214 	 * @since 2.3.0
       
   215 	 * @access public
       
   216 	 * @var string
       
   217 	 */
       
   218 	var $usermeta;
       
   219 
       
   220 	/**
       
   221 	 * WordPress Terms table
       
   222 	 *
       
   223 	 * @since 2.3.0
       
   224 	 * @access public
       
   225 	 * @var string
       
   226 	 */
       
   227 	var $terms;
       
   228 
       
   229 	/**
       
   230 	 * WordPress Term Taxonomy table
       
   231 	 *
       
   232 	 * @since 2.3.0
       
   233 	 * @access public
       
   234 	 * @var string
       
   235 	 */
       
   236 	var $term_taxonomy;
       
   237 
       
   238 	/**
       
   239 	 * WordPress Term Relationships table
       
   240 	 *
       
   241 	 * @since 2.3.0
       
   242 	 * @access public
       
   243 	 * @var string
       
   244 	 */
       
   245 	var $term_relationships;
       
   246 
       
   247 	/**
       
   248 	 * List of WordPress tables
       
   249 	 *
       
   250 	 * @since {@internal Version Unknown}}
       
   251 	 * @access private
       
   252 	 * @var array
       
   253 	 */
       
   254 	var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
       
   255 			'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
       
   256 
       
   257 	/**
       
   258 	 * Format specifiers for DB columns. Columns not listed here default to %s.  Initialized in wp-settings.php.
       
   259 	 *
       
   260 	 * Keys are colmn names, values are format types: 'ID' => '%d'
       
   261 	 *
       
   262 	 * @since 2.8.0
       
   263 	 * @see wpdb:prepare()
       
   264 	 * @see wpdb:insert()
       
   265 	 * @see wpdb:update()
       
   266 	 * @access public
       
   267 	 * @war array
       
   268 	 */
       
   269 	var $field_types = array();
       
   270 
       
   271 	/**
       
   272 	 * Database table columns charset
       
   273 	 *
       
   274 	 * @since 2.2.0
       
   275 	 * @access public
       
   276 	 * @var string
       
   277 	 */
       
   278 	var $charset;
       
   279 
       
   280 	/**
       
   281 	 * Database table columns collate
       
   282 	 *
       
   283 	 * @since 2.2.0
       
   284 	 * @access public
       
   285 	 * @var string
       
   286 	 */
       
   287 	var $collate;
       
   288 
       
   289 	/**
       
   290 	 * Whether to use mysql_real_escape_string
       
   291 	 *
       
   292 	 * @since 2.8.0
       
   293 	 * @access public
       
   294 	 * @var bool
       
   295 	 */
       
   296 	var $real_escape = false;
       
   297 
       
   298 	/**
       
   299 	 * Connects to the database server and selects a database
       
   300 	 *
       
   301 	 * PHP4 compatibility layer for calling the PHP5 constructor.
       
   302 	 *
       
   303 	 * @uses wpdb::__construct() Passes parameters and returns result
       
   304 	 * @since 0.71
       
   305 	 *
       
   306 	 * @param string $dbuser MySQL database user
       
   307 	 * @param string $dbpassword MySQL database password
       
   308 	 * @param string $dbname MySQL database name
       
   309 	 * @param string $dbhost MySQL database host
       
   310 	 */
       
   311 	function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
       
   312 		return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
       
   313 	}
       
   314 
       
   315 	/**
       
   316 	 * Connects to the database server and selects a database
       
   317 	 *
       
   318 	 * PHP5 style constructor for compatibility with PHP5. Does
       
   319 	 * the actual setting up of the class properties and connection
       
   320 	 * to the database.
       
   321 	 *
       
   322 	 * @since 2.0.8
       
   323 	 *
       
   324 	 * @param string $dbuser MySQL database user
       
   325 	 * @param string $dbpassword MySQL database password
       
   326 	 * @param string $dbname MySQL database name
       
   327 	 * @param string $dbhost MySQL database host
       
   328 	 */
       
   329 	function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
       
   330 		register_shutdown_function(array(&$this, "__destruct"));
       
   331 
       
   332 		if ( defined('WP_DEBUG') and WP_DEBUG == true )
       
   333 			$this->show_errors();
       
   334 
       
   335 		if ( defined('DB_CHARSET') )
       
   336 			$this->charset = DB_CHARSET;
       
   337 
       
   338 		if ( defined('DB_COLLATE') )
       
   339 			$this->collate = DB_COLLATE;
       
   340 
       
   341 		$this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
       
   342 		if (!$this->dbh) {
       
   343 			$this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/'
       
   344 <h1>Erreur lors de l&rsquo;établissement de la connexion &agrave; la base de données</h1>
       
   345 <p>Cela signifie soit que l&rsquo;identifiant et/ou le mot de passe indiqués dans votre fichier <code>wp-config.php</code> sont incorrects, ou que le serveur de base de données à l&rsquo;adresse <code>%s</code> est inaccessible - ce qui implique que le serveur de base de données de votre hébergeur est sans doute défaillant.</p>
       
   346 <ul>
       
   347 	<li>Êtes-vous certain(e) d&rsquo;avoir correctement indiquĂ© votre identifiant et votre mot de passe&nbsp;?</li>
       
   348 	<li>Êtes-vous certain(e) d&rsquo;avoir entrĂ© le bon serveur de base de donnĂ©es&nbsp;?</li>
       
   349 	<li>Êtes-vous certain(e) que le serveur de base de donnĂ©es fonctionne correctement&nbsp;?</li>
       
   350 </ul>
       
   351 <p>Si vous n&rsquo;ĂȘtes pas sĂ»r(e) de bien comprendre les mots de cette liste, vous devriez sans doute prendre contact avec votre hĂ©bergeur. Si malgrĂ© cela cette erreur s&rsquo;affiche toujours, indiquez votre problĂšme au <a href=\'http://www.wordpress-fr.net/support/\'>forum d&rsquo;entraide en français</a>.</p>
       
   352 '/*/WP_I18N_DB_CONN_ERROR*/, $dbhost));
       
   353 			return;
       
   354 		}
       
   355 
       
   356 		$this->ready = true;
       
   357 
       
   358 		if ( $this->has_cap( 'collation' ) ) {
       
   359 			if ( !empty($this->charset) ) {
       
   360 				if ( function_exists('mysql_set_charset') ) {
       
   361 					mysql_set_charset($this->charset, $this->dbh);
       
   362 					$this->real_escape = true;
       
   363 				} else {
       
   364 					$collation_query = "SET NAMES '{$this->charset}'";
       
   365 					if ( !empty($this->collate) )
       
   366 						$collation_query .= " COLLATE '{$this->collate}'";
       
   367 					$this->query($collation_query);
       
   368 				}
       
   369 			}
       
   370 		}
       
   371 
       
   372 		$this->select($dbname);
       
   373 	}
       
   374 
       
   375 	/**
       
   376 	 * PHP5 style destructor and will run when database object is destroyed.
       
   377 	 *
       
   378 	 * @since 2.0.8
       
   379 	 *
       
   380 	 * @return bool Always true
       
   381 	 */
       
   382 	function __destruct() {
       
   383 		return true;
       
   384 	}
       
   385 
       
   386 	/**
       
   387 	 * Sets the table prefix for the WordPress tables.
       
   388 	 *
       
   389 	 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
       
   390 	 * override the WordPress users and usersmeta tables that would otherwise be determined by the $prefix.
       
   391 	 *
       
   392 	 * @since 2.5.0
       
   393 	 *
       
   394 	 * @param string $prefix Alphanumeric name for the new prefix.
       
   395 	 * @return string|WP_Error Old prefix or WP_Error on error
       
   396 	 */
       
   397 	function set_prefix($prefix) {
       
   398 
       
   399 		if ( preg_match('|[^a-z0-9_]|i', $prefix) )
       
   400 			return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Préfixe de la base de données invalide'/*/WP_I18N_DB_BAD_PREFIX*/);
       
   401 
       
   402 		$old_prefix = $this->prefix;
       
   403 		$this->prefix = $prefix;
       
   404 
       
   405 		foreach ( (array) $this->tables as $table )
       
   406 			$this->$table = $this->prefix . $table;
       
   407 
       
   408 		if ( defined('CUSTOM_USER_TABLE') )
       
   409 			$this->users = CUSTOM_USER_TABLE;
       
   410 
       
   411 		if ( defined('CUSTOM_USER_META_TABLE') )
       
   412 			$this->usermeta = CUSTOM_USER_META_TABLE;
       
   413 
       
   414 		return $old_prefix;
       
   415 	}
       
   416 
       
   417 	/**
       
   418 	 * Selects a database using the current database connection.
       
   419 	 *
       
   420 	 * The database name will be changed based on the current database
       
   421 	 * connection. On failure, the execution will bail and display an DB error.
       
   422 	 *
       
   423 	 * @since 0.71
       
   424 	 *
       
   425 	 * @param string $db MySQL database name
       
   426 	 * @return null Always null.
       
   427 	 */
       
   428 	function select($db) {
       
   429 		if (!@mysql_select_db($db, $this->dbh)) {
       
   430 			$this->ready = false;
       
   431 			$this->bail(sprintf(/*WP_I18N_DB_SELECT_DB*/'
       
   432 <h1>Impossible de sélectionner la base de données</h1>
       
   433 <p>La connexion au serveur de base de donnĂ©es s&rsquo;est bien faite (donc votre identifiant et votre mot de passe sont les bons), mais la base de donnĂ©es <code>%1$s</code> n&rsquo;a pas pu ĂȘtre sĂ©lectionnĂ©e.</p>
       
   434 <ul>
       
   435 <li>Êtes-vous certain(e) qu&rsquo;elle existe&nbsp;?</li>
       
   436 <li>L&rsquo;utilisateur <code>%2$s</code> a-t-il les droits pour utiliser la base de donn&eacute; <code>%1$s</code>&nbsp;?</li>
       
   437 <li>Sur certains systĂšmes, le nom de votre base de donnĂ©es est prĂ©fixĂ©e de votre identifiant, donc son nom complet peut ĂȘtre <code>identifiant_nomdelabase</code>. Cela peut-il ĂȘtre la cause du problĂšme&nbps;?</li>
       
   438 </ul>
       
   439 <p>Si vous ne savez pas comment rĂ©gler votre base de donnĂ©es, vous devriez <strong>prendre contact avec votre hĂ©bergeur</strong>. Si toutes vos tentatives Ă©chouent, peut-ĂȘtre trouverez-vous une rĂ©ponse sur le <a href=\'http://www.wordpress-fr.net/support/\'>forum d&rsquo;entraide en français</a>.</p>'/*/WP_I18N_DB_SELECT_DB*/, $db, DB_USER));
       
   440 			return;
       
   441 		}
       
   442 	}
       
   443 
       
   444 	function _weak_escape($string) {
       
   445 		return addslashes($string);
       
   446 	}
       
   447 
       
   448 	function _real_escape($string) {
       
   449 		if ( $this->dbh && $this->real_escape )
       
   450 			return mysql_real_escape_string( $string, $this->dbh );
       
   451 		else
       
   452 			return addslashes( $string );
       
   453 	}
       
   454 
       
   455 	function _escape($data) {
       
   456 		if ( is_array($data) ) {
       
   457 			foreach ( (array) $data as $k => $v ) {
       
   458 				if ( is_array($v) )
       
   459 					$data[$k] = $this->_escape( $v );
       
   460 				else
       
   461 					$data[$k] = $this->_real_escape( $v );
       
   462 			}
       
   463 		} else {
       
   464 			$data = $this->_real_escape( $data );
       
   465 		}
       
   466 
       
   467 		return $data;
       
   468 	}
       
   469 
       
   470 	/**
       
   471 	 * Escapes content for insertion into the database using addslashes(), for security
       
   472 	 *
       
   473 	 * @since 0.71
       
   474 	 *
       
   475 	 * @param string|array $data
       
   476 	 * @return string query safe string
       
   477 	 */
       
   478 	function escape($data) {
       
   479 		if ( is_array($data) ) {
       
   480 			foreach ( (array) $data as $k => $v ) {
       
   481 				if ( is_array($v) )
       
   482 					$data[$k] = $this->escape( $v );
       
   483 				else
       
   484 					$data[$k] = $this->_weak_escape( $v );
       
   485 			}
       
   486 		} else {
       
   487 			$data = $this->_weak_escape( $data );
       
   488 		}
       
   489 
       
   490 		return $data;
       
   491 	}
       
   492 
       
   493 	/**
       
   494 	 * Escapes content by reference for insertion into the database, for security
       
   495 	 *
       
   496 	 * @since 2.3.0
       
   497 	 *
       
   498 	 * @param string $s
       
   499 	 */
       
   500 	function escape_by_ref(&$string) {
       
   501 		$string = $this->_real_escape( $string );
       
   502 	}
       
   503 
       
   504 	/**
       
   505 	 * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
       
   506 	 *
       
   507 	 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
       
   508 	 * Does not support sign, padding, alignment, width or precision specifiers.
       
   509 	 * Does not support argument numbering/swapping.
       
   510 	 *
       
   511 	 * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}.
       
   512 	 *
       
   513 	 * Both %d and %s should be left unquoted in the query string.
       
   514 	 *
       
   515 	 * <code>
       
   516 	 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 )
       
   517 	 * </code>
       
   518 	 *
       
   519 	 * @link http://php.net/sprintf Description of syntax.
       
   520 	 * @since 2.3.0
       
   521 	 *
       
   522 	 * @param string $query Query statement with sprintf()-like placeholders
       
   523 	 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
       
   524 	 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
       
   525 	 * @return null|string Sanitized query string
       
   526 	 */
       
   527 	function prepare($query = null) { // ( $query, *$args )
       
   528 		if ( is_null( $query ) )
       
   529 			return;
       
   530 		$args = func_get_args();
       
   531 		array_shift($args);
       
   532 		// If args were passed as an array (as in vsprintf), move them up
       
   533 		if ( isset($args[0]) && is_array($args[0]) )
       
   534 			$args = $args[0];
       
   535 		$query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
       
   536 		$query = str_replace('"%s"', '%s', $query); // doublequote unquoting
       
   537 		$query = str_replace('%s', "'%s'", $query); // quote the strings
       
   538 		array_walk($args, array(&$this, 'escape_by_ref'));
       
   539 		return @vsprintf($query, $args);
       
   540 	}
       
   541 
       
   542 	/**
       
   543 	 * Print SQL/DB error.
       
   544 	 *
       
   545 	 * @since 0.71
       
   546 	 * @global array $EZSQL_ERROR Stores error information of query and error string
       
   547 	 *
       
   548 	 * @param string $str The error to display
       
   549 	 * @return bool False if the showing of errors is disabled.
       
   550 	 */
       
   551 	function print_error($str = '') {
       
   552 		global $EZSQL_ERROR;
       
   553 
       
   554 		if (!$str) $str = mysql_error($this->dbh);
       
   555 		$EZSQL_ERROR[] = array ('query' => $this->last_query, 'error_str' => $str);
       
   556 
       
   557 		if ( $this->suppress_errors )
       
   558 			return false;
       
   559 
       
   560 		if ( $caller = $this->get_caller() )
       
   561 			$error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR_FULL*/'Erreur de la base de donnĂ©es WordPress %1$s pour la requĂȘte %2$s faite par %3$s'/*/WP_I18N_DB_QUERY_ERROR_FULL*/, $str, $this->last_query, $caller);
       
   562 		else
       
   563 			$error_str = sprintf(/*WP_I18N_DB_QUERY_ERROR*/'Erreur de la base de donnĂ©es WordPress %1$s pour la requĂȘte %2$s'/*/WP_I18N_DB_QUERY_ERROR*/, $str, $this->last_query);
       
   564 
       
   565 		$log_error = true;
       
   566 		if ( ! function_exists('error_log') )
       
   567 			$log_error = false;
       
   568 
       
   569 		$log_file = @ini_get('error_log');
       
   570 		if ( !empty($log_file) && ('syslog' != $log_file) && !@is_writable($log_file) )
       
   571 			$log_error = false;
       
   572 
       
   573 		if ( $log_error )
       
   574 			@error_log($error_str, 0);
       
   575 
       
   576 		// Is error output turned on or not..
       
   577 		if ( !$this->show_errors )
       
   578 			return false;
       
   579 
       
   580 		$str = htmlspecialchars($str, ENT_QUOTES);
       
   581 		$query = htmlspecialchars($this->last_query, ENT_QUOTES);
       
   582 
       
   583 		// If there is an error then take note of it
       
   584 		print "<div id='error'>
       
   585 		<p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
       
   586 		<code>$query</code></p>
       
   587 		</div>";
       
   588 	}
       
   589 
       
   590 	/**
       
   591 	 * Enables showing of database errors.
       
   592 	 *
       
   593 	 * This function should be used only to enable showing of errors.
       
   594 	 * wpdb::hide_errors() should be used instead for hiding of errors. However,
       
   595 	 * this function can be used to enable and disable showing of database
       
   596 	 * errors.
       
   597 	 *
       
   598 	 * @since 0.71
       
   599 	 *
       
   600 	 * @param bool $show Whether to show or hide errors
       
   601 	 * @return bool Old value for showing errors.
       
   602 	 */
       
   603 	function show_errors( $show = true ) {
       
   604 		$errors = $this->show_errors;
       
   605 		$this->show_errors = $show;
       
   606 		return $errors;
       
   607 	}
       
   608 
       
   609 	/**
       
   610 	 * Disables showing of database errors.
       
   611 	 *
       
   612 	 * @since 0.71
       
   613 	 *
       
   614 	 * @return bool Whether showing of errors was active or not
       
   615 	 */
       
   616 	function hide_errors() {
       
   617 		$show = $this->show_errors;
       
   618 		$this->show_errors = false;
       
   619 		return $show;
       
   620 	}
       
   621 
       
   622 	/**
       
   623 	 * Whether to suppress database errors.
       
   624 	 *
       
   625 	 * @param unknown_type $suppress
       
   626 	 * @return unknown
       
   627 	 */
       
   628 	function suppress_errors( $suppress = true ) {
       
   629 		$errors = $this->suppress_errors;
       
   630 		$this->suppress_errors = $suppress;
       
   631 		return $errors;
       
   632 	}
       
   633 
       
   634 	/**
       
   635 	 * Kill cached query results.
       
   636 	 *
       
   637 	 * @since 0.71
       
   638 	 */
       
   639 	function flush() {
       
   640 		$this->last_result = array();
       
   641 		$this->col_info = null;
       
   642 		$this->last_query = null;
       
   643 	}
       
   644 
       
   645 	/**
       
   646 	 * Perform a MySQL database query, using current database connection.
       
   647 	 *
       
   648 	 * More information can be found on the codex page.
       
   649 	 *
       
   650 	 * @since 0.71
       
   651 	 *
       
   652 	 * @param string $query
       
   653 	 * @return int|false Number of rows affected/selected or false on error
       
   654 	 */
       
   655 	function query($query) {
       
   656 		if ( ! $this->ready )
       
   657 			return false;
       
   658 
       
   659 		// filter the query, if filters are available
       
   660 		// NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
       
   661 		if ( function_exists('apply_filters') )
       
   662 			$query = apply_filters('query', $query);
       
   663 
       
   664 		// initialise return
       
   665 		$return_val = 0;
       
   666 		$this->flush();
       
   667 
       
   668 		// Log how the function was called
       
   669 		$this->func_call = "\$db->query(\"$query\")";
       
   670 
       
   671 		// Keep track of the last query for debug..
       
   672 		$this->last_query = $query;
       
   673 
       
   674 		// Perform the query via std mysql_query function..
       
   675 		if ( defined('SAVEQUERIES') && SAVEQUERIES )
       
   676 			$this->timer_start();
       
   677 
       
   678 		$this->result = @mysql_query($query, $this->dbh);
       
   679 		++$this->num_queries;
       
   680 
       
   681 		if ( defined('SAVEQUERIES') && SAVEQUERIES )
       
   682 			$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
       
   683 
       
   684 		// If there is an error then take note of it..
       
   685 		if ( $this->last_error = mysql_error($this->dbh) ) {
       
   686 			$this->print_error();
       
   687 			return false;
       
   688 		}
       
   689 
       
   690 		if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
       
   691 			$this->rows_affected = mysql_affected_rows($this->dbh);
       
   692 			// Take note of the insert_id
       
   693 			if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
       
   694 				$this->insert_id = mysql_insert_id($this->dbh);
       
   695 			}
       
   696 			// Return number of rows affected
       
   697 			$return_val = $this->rows_affected;
       
   698 		} else {
       
   699 			$i = 0;
       
   700 			while ($i < @mysql_num_fields($this->result)) {
       
   701 				$this->col_info[$i] = @mysql_fetch_field($this->result);
       
   702 				$i++;
       
   703 			}
       
   704 			$num_rows = 0;
       
   705 			while ( $row = @mysql_fetch_object($this->result) ) {
       
   706 				$this->last_result[$num_rows] = $row;
       
   707 				$num_rows++;
       
   708 			}
       
   709 
       
   710 			@mysql_free_result($this->result);
       
   711 
       
   712 			// Log number of rows the query returned
       
   713 			$this->num_rows = $num_rows;
       
   714 
       
   715 			// Return number of rows selected
       
   716 			$return_val = $this->num_rows;
       
   717 		}
       
   718 
       
   719 		return $return_val;
       
   720 	}
       
   721 
       
   722 	/**
       
   723 	 * Insert a row into a table.
       
   724 	 *
       
   725 	 * <code>
       
   726 	 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
       
   727 	 * </code>
       
   728 	 *
       
   729 	 * @since 2.5.0
       
   730 	 * @see wpdb::prepare()
       
   731 	 *
       
   732 	 * @param string $table table name
       
   733 	 * @param array $data Data to insert (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
       
   734 	 * @param array|string $format (optional) An array of formats to be mapped to each of the value in $data.  If string, that format will be used for all of the values in $data.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $data will be treated as strings.
       
   735 	 * @return int|false The number of rows inserted, or false on error.
       
   736 	 */
       
   737 	function insert($table, $data, $format = null) {
       
   738 		$formats = $format = (array) $format;
       
   739 		$fields = array_keys($data);
       
   740 		$formatted_fields = array();
       
   741 		foreach ( $fields as $field ) {
       
   742 			if ( !empty($format) )
       
   743 				$form = ( $form = array_shift($formats) ) ? $form : $format[0];
       
   744 			elseif ( isset($this->field_types[$field]) )
       
   745 				$form = $this->field_types[$field];
       
   746 			else
       
   747 				$form = '%s';
       
   748 			$formatted_fields[] = $form;
       
   749 		}
       
   750 		$sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')";
       
   751 		return $this->query( $this->prepare( $sql, $data) );
       
   752 	}
       
   753 
       
   754 
       
   755 	/**
       
   756 	 * Update a row in the table
       
   757 	 *
       
   758 	 * <code>
       
   759 	 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
       
   760 	 * </code>
       
   761 	 *
       
   762 	 * @since 2.5.0
       
   763 	 * @see wpdb::prepare()
       
   764 	 *
       
   765 	 * @param string $table table name
       
   766 	 * @param array $data Data to update (in column => value pairs).  Both $data columns and $data values should be "raw" (neither should be SQL escaped).
       
   767 	 * @param array $where A named array of WHERE clauses (in column => value pairs).  Multiple clauses will be joined with ANDs.  Both $where columns and $where values should be "raw".
       
   768 	 * @param array|string $format (optional) An array of formats to be mapped to each of the values in $data.  If string, that format will be used for all of the values in $data.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $data will be treated as strings.
       
   769 	 * @param array|string $format_where (optional) An array of formats to be mapped to each of the values in $where.  If string, that format will be used for all of  the items in $where.  A format is one of '%d', '%s' (decimal number, string).  If omitted, all values in $where will be treated as strings.
       
   770 	 * @return int|false The number of rows updated, or false on error.
       
   771 	 */
       
   772 	function update($table, $data, $where, $format = null, $where_format = null) {
       
   773 		if ( !is_array( $where ) )
       
   774 			return false;
       
   775 
       
   776 		$formats = $format = (array) $format;
       
   777 		$bits = $wheres = array();
       
   778 		foreach ( (array) array_keys($data) as $field ) {
       
   779 			if ( !empty($format) )
       
   780 				$form = ( $form = array_shift($formats) ) ? $form : $format[0];
       
   781 			elseif ( isset($this->field_types[$field]) )
       
   782 				$form = $this->field_types[$field];
       
   783 			else
       
   784 				$form = '%s';
       
   785 			$bits[] = "`$field` = {$form}";
       
   786 		}
       
   787 
       
   788 		$where_formats = $where_format = (array) $where_format;
       
   789 		foreach ( (array) array_keys($where) as $field ) {
       
   790 			if ( !empty($where_format) )
       
   791 				$form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0];
       
   792 			elseif ( isset($this->field_types[$field]) )
       
   793 				$form = $this->field_types[$field];
       
   794 			else
       
   795 				$form = '%s';
       
   796 			$wheres[] = "`$field` = {$form}";
       
   797 		}
       
   798 
       
   799 		$sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
       
   800 		return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) );
       
   801 	}
       
   802 
       
   803 	/**
       
   804 	 * Retrieve one variable from the database.
       
   805 	 *
       
   806 	 * Executes a SQL query and returns the value from the SQL result.
       
   807 	 * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified.
       
   808 	 * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
       
   809 	 *
       
   810 	 * @since 0.71
       
   811 	 *
       
   812 	 * @param string|null $query SQL query.  If null, use the result from the previous query.
       
   813 	 * @param int $x (optional) Column of value to return.  Indexed from 0.
       
   814 	 * @param int $y (optional) Row of value to return.  Indexed from 0.
       
   815 	 * @return string Database query result
       
   816 	 */
       
   817 	function get_var($query=null, $x = 0, $y = 0) {
       
   818 		$this->func_call = "\$db->get_var(\"$query\",$x,$y)";
       
   819 		if ( $query )
       
   820 			$this->query($query);
       
   821 
       
   822 		// Extract var out of cached results based x,y vals
       
   823 		if ( !empty( $this->last_result[$y] ) ) {
       
   824 			$values = array_values(get_object_vars($this->last_result[$y]));
       
   825 		}
       
   826 
       
   827 		// If there is a value return it else return null
       
   828 		return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null;
       
   829 	}
       
   830 
       
   831 	/**
       
   832 	 * Retrieve one row from the database.
       
   833 	 *
       
   834 	 * Executes a SQL query and returns the row from the SQL result.
       
   835 	 *
       
   836 	 * @since 0.71
       
   837 	 *
       
   838 	 * @param string|null $query SQL query.
       
   839 	 * @param string $output (optional) one of ARRAY_A | ARRAY_N | OBJECT constants.  Return an associative array (column => value, ...), a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
       
   840 	 * @param int $y (optional) Row to return.  Indexed from 0.
       
   841 	 * @return mixed Database query result in format specifed by $output
       
   842 	 */
       
   843 	function get_row($query = null, $output = OBJECT, $y = 0) {
       
   844 		$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
       
   845 		if ( $query )
       
   846 			$this->query($query);
       
   847 		else
       
   848 			return null;
       
   849 
       
   850 		if ( !isset($this->last_result[$y]) )
       
   851 			return null;
       
   852 
       
   853 		if ( $output == OBJECT ) {
       
   854 			return $this->last_result[$y] ? $this->last_result[$y] : null;
       
   855 		} elseif ( $output == ARRAY_A ) {
       
   856 			return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
       
   857 		} elseif ( $output == ARRAY_N ) {
       
   858 			return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
       
   859 		} else {
       
   860 			$this->print_error(/*WP_I18N_DB_GETROW_ERROR*/' $db->get_row(string query, output type, int offset) -- Le type de sortie doit ĂȘtre: OBJECT, ARRAY_A, ARRAY_N'/*/WP_I18N_DB_GETROW_ERROR*/);
       
   861 		}
       
   862 	}
       
   863 
       
   864 	/**
       
   865 	 * Retrieve one column from the database.
       
   866 	 *
       
   867 	 * Executes a SQL query and returns the column from the SQL result.
       
   868 	 * If the SQL result contains more than one column, this function returns the column specified.
       
   869 	 * If $query is null, this function returns the specified column from the previous SQL result.
       
   870 	 *
       
   871 	 * @since 0.71
       
   872 	 *
       
   873 	 * @param string|null $query SQL query.  If null, use the result from the previous query.
       
   874 	 * @param int $x Column to return.  Indexed from 0.
       
   875 	 * @return array Database query result.  Array indexed from 0 by SQL result row number.
       
   876 	 */
       
   877 	function get_col($query = null , $x = 0) {
       
   878 		if ( $query )
       
   879 			$this->query($query);
       
   880 
       
   881 		$new_array = array();
       
   882 		// Extract the column values
       
   883 		for ( $i=0; $i < count($this->last_result); $i++ ) {
       
   884 			$new_array[$i] = $this->get_var(null, $x, $i);
       
   885 		}
       
   886 		return $new_array;
       
   887 	}
       
   888 
       
   889 	/**
       
   890 	 * Retrieve an entire SQL result set from the database (i.e., many rows)
       
   891 	 *
       
   892 	 * Executes a SQL query and returns the entire SQL result.
       
   893 	 *
       
   894 	 * @since 0.71
       
   895 	 *
       
   896 	 * @param string $query SQL query.
       
   897 	 * @param string $output (optional) ane of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.  With one of the first three, return an array of rows indexed from 0 by SQL result row number.  Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively.  With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value.  Duplicate keys are discarded.
       
   898 	 * @return mixed Database query results
       
   899 	 */
       
   900 	function get_results($query = null, $output = OBJECT) {
       
   901 		$this->func_call = "\$db->get_results(\"$query\", $output)";
       
   902 
       
   903 		if ( $query )
       
   904 			$this->query($query);
       
   905 		else
       
   906 			return null;
       
   907 
       
   908 		if ( $output == OBJECT ) {
       
   909 			// Return an integer-keyed array of row objects
       
   910 			return $this->last_result;
       
   911 		} elseif ( $output == OBJECT_K ) {
       
   912 			// Return an array of row objects with keys from column 1
       
   913 			// (Duplicates are discarded)
       
   914 			foreach ( $this->last_result as $row ) {
       
   915 				$key = array_shift( get_object_vars( $row ) );
       
   916 				if ( !isset( $new_array[ $key ] ) )
       
   917 					$new_array[ $key ] = $row;
       
   918 			}
       
   919 			return $new_array;
       
   920 		} elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
       
   921 			// Return an integer-keyed array of...
       
   922 			if ( $this->last_result ) {
       
   923 				$i = 0;
       
   924 				foreach( (array) $this->last_result as $row ) {
       
   925 					if ( $output == ARRAY_N ) {
       
   926 						// ...integer-keyed row arrays
       
   927 						$new_array[$i] = array_values( get_object_vars( $row ) );
       
   928 					} else {
       
   929 						// ...column name-keyed row arrays
       
   930 						$new_array[$i] = get_object_vars( $row );
       
   931 					}
       
   932 					++$i;
       
   933 				}
       
   934 				return $new_array;
       
   935 			}
       
   936 		}
       
   937 	}
       
   938 
       
   939 	/**
       
   940 	 * Retrieve column metadata from the last query.
       
   941 	 *
       
   942 	 * @since 0.71
       
   943 	 *
       
   944 	 * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
       
   945 	 * @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
       
   946 	 * @return mixed Column Results
       
   947 	 */
       
   948 	function get_col_info($info_type = 'name', $col_offset = -1) {
       
   949 		if ( $this->col_info ) {
       
   950 			if ( $col_offset == -1 ) {
       
   951 				$i = 0;
       
   952 				foreach( (array) $this->col_info as $col ) {
       
   953 					$new_array[$i] = $col->{$info_type};
       
   954 					$i++;
       
   955 				}
       
   956 				return $new_array;
       
   957 			} else {
       
   958 				return $this->col_info[$col_offset]->{$info_type};
       
   959 			}
       
   960 		}
       
   961 	}
       
   962 
       
   963 	/**
       
   964 	 * Starts the timer, for debugging purposes.
       
   965 	 *
       
   966 	 * @since 1.5.0
       
   967 	 *
       
   968 	 * @return true
       
   969 	 */
       
   970 	function timer_start() {
       
   971 		$mtime = microtime();
       
   972 		$mtime = explode(' ', $mtime);
       
   973 		$this->time_start = $mtime[1] + $mtime[0];
       
   974 		return true;
       
   975 	}
       
   976 
       
   977 	/**
       
   978 	 * Stops the debugging timer.
       
   979 	 *
       
   980 	 * @since 1.5.0
       
   981 	 *
       
   982 	 * @return int Total time spent on the query, in milliseconds
       
   983 	 */
       
   984 	function timer_stop() {
       
   985 		$mtime = microtime();
       
   986 		$mtime = explode(' ', $mtime);
       
   987 		$time_end = $mtime[1] + $mtime[0];
       
   988 		$time_total = $time_end - $this->time_start;
       
   989 		return $time_total;
       
   990 	}
       
   991 
       
   992 	/**
       
   993 	 * Wraps errors in a nice header and footer and dies.
       
   994 	 *
       
   995 	 * Will not die if wpdb::$show_errors is true
       
   996 	 *
       
   997 	 * @since 1.5.0
       
   998 	 *
       
   999 	 * @param string $message
       
  1000 	 * @return false|void
       
  1001 	 */
       
  1002 	function bail($message) {
       
  1003 		if ( !$this->show_errors ) {
       
  1004 			if ( class_exists('WP_Error') )
       
  1005 				$this->error = new WP_Error('500', $message);
       
  1006 			else
       
  1007 				$this->error = $message;
       
  1008 			return false;
       
  1009 		}
       
  1010 		wp_die($message);
       
  1011 	}
       
  1012 
       
  1013 	/**
       
  1014 	 * Whether or not MySQL database is at least the required minimum version.
       
  1015 	 *
       
  1016 	 * @since 2.5.0
       
  1017 	 * @uses $wp_version
       
  1018 	 *
       
  1019 	 * @return WP_Error
       
  1020 	 */
       
  1021 	function check_database_version()
       
  1022 	{
       
  1023 		global $wp_version;
       
  1024 		// Make sure the server has MySQL 4.0
       
  1025 		if ( version_compare($this->db_version(), '4.0.0', '<') )
       
  1026 			return new WP_Error('database_version',sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $wp_version));
       
  1027 	}
       
  1028 
       
  1029 	/**
       
  1030 	 * Whether of not the database supports collation.
       
  1031 	 *
       
  1032 	 * Called when WordPress is generating the table scheme.
       
  1033 	 *
       
  1034 	 * @since 2.5.0
       
  1035 	 *
       
  1036 	 * @return bool True if collation is supported, false if version does not
       
  1037 	 */
       
  1038 	function supports_collation()
       
  1039 	{
       
  1040 		return $this->has_cap( 'collation' );
       
  1041 	}
       
  1042 
       
  1043 	/**
       
  1044 	 * Generic function to determine if a database supports a particular feature
       
  1045 	 * @param string $db_cap the feature
       
  1046 	 * @param false|string|resource $dbh_or_table (not implemented) Which database to test.  False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
       
  1047 	 * @return bool
       
  1048 	 */
       
  1049 	function has_cap( $db_cap ) {
       
  1050 		$version = $this->db_version();
       
  1051 
       
  1052 		switch ( strtolower( $db_cap ) ) :
       
  1053 		case 'collation' :    // @since 2.5.0
       
  1054 		case 'group_concat' : // @since 2.7
       
  1055 		case 'subqueries' :   // @since 2.7
       
  1056 			return version_compare($version, '4.1', '>=');
       
  1057 			break;
       
  1058 		endswitch;
       
  1059 
       
  1060 		return false;
       
  1061 	}
       
  1062 
       
  1063 	/**
       
  1064 	 * Retrieve the name of the function that called wpdb.
       
  1065 	 *
       
  1066 	 * Requires PHP 4.3 and searches up the list of functions until it reaches
       
  1067 	 * the one that would most logically had called this method.
       
  1068 	 *
       
  1069 	 * @since 2.5.0
       
  1070 	 *
       
  1071 	 * @return string The name of the calling function
       
  1072 	 */
       
  1073 	function get_caller() {
       
  1074 		// requires PHP 4.3+
       
  1075 		if ( !is_callable('debug_backtrace') )
       
  1076 			return '';
       
  1077 
       
  1078 		$bt = debug_backtrace();
       
  1079 		$caller = array();
       
  1080 
       
  1081 		$bt = array_reverse( $bt );
       
  1082 		foreach ( (array) $bt as $call ) {
       
  1083 			if ( @$call['class'] == __CLASS__ )
       
  1084 				continue;
       
  1085 			$function = $call['function'];
       
  1086 			if ( isset( $call['class'] ) )
       
  1087 				$function = $call['class'] . "->$function";
       
  1088 			$caller[] = $function;
       
  1089 		}
       
  1090 		$caller = join( ', ', $caller );
       
  1091 
       
  1092 		return $caller;
       
  1093 	}
       
  1094 
       
  1095 	/**
       
  1096 	 * The database version number
       
  1097 	 * @param false|string|resource $dbh_or_table (not implemented) Which database to test.  False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
       
  1098 	 * @return false|string false on failure, version number on success
       
  1099 	 */
       
  1100 	function db_version() {
       
  1101 		return preg_replace('/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ));
       
  1102 	}
       
  1103 }
       
  1104 
       
  1105 if ( ! isset($wpdb) ) {
       
  1106 	/**
       
  1107 	 * WordPress Database Object, if it isn't set already in wp-content/db.php
       
  1108 	 * @global object $wpdb Creates a new wpdb object based on wp-config.php Constants for the database
       
  1109 	 * @since 0.71
       
  1110 	 */
       
  1111 	$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
       
  1112 }
       
  1113 ?>