wp/wp-includes/wp-db.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * WordPress DB Class
     3  * WordPress database access abstraction class
     4  *
     4  *
     5  * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
     5  * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage Database
     8  * @subpackage Database
    35  * @since 0.71
    35  * @since 0.71
    36  */
    36  */
    37 define( 'ARRAY_N', 'ARRAY_N' );
    37 define( 'ARRAY_N', 'ARRAY_N' );
    38 
    38 
    39 /**
    39 /**
    40  * WordPress Database Access Abstraction Object
    40  * WordPress database access abstraction class.
    41  *
    41  *
    42  * It is possible to replace this class with your own
    42  * This class is used to interact with a database without needing to use raw SQL statements.
    43  * by setting the $wpdb global variable in wp-content/db.php
    43  * By default, WordPress uses this class to instantiate the global $wpdb object, providing
    44  * file to your class. The wpdb class will still be included,
    44  * access to the WordPress database.
    45  * so you can extend it or simply use your own.
       
    46  *
    45  *
    47  * @link https://codex.wordpress.org/Function_Reference/wpdb_Class
    46  * It is possible to replace this class with your own by setting the $wpdb global variable
       
    47  * in wp-content/db.php file to your class. The wpdb class will still be included, so you can
       
    48  * extend it or simply use your own.
       
    49  *
       
    50  * @link https://developer.wordpress.org/reference/classes/wpdb/
    48  *
    51  *
    49  * @since 0.71
    52  * @since 0.71
    50  */
    53  */
    51 class wpdb {
    54 class wpdb {
    52 
    55 
    53 	/**
    56 	/**
    54 	 * Whether to show SQL/DB errors.
    57 	 * Whether to show SQL/DB errors.
    55 	 *
    58 	 *
    56 	 * Default behavior is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY
    59 	 * Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true.
    57 	 * evaluated to true.
       
    58 	 *
    60 	 *
    59 	 * @since 0.71
    61 	 * @since 0.71
    60 	 * @var bool
    62 	 * @var bool
    61 	 */
    63 	 */
    62 	var $show_errors = false;
    64 	var $show_errors = false;
    63 
    65 
    64 	/**
    66 	/**
    65 	 * Whether to suppress errors during the DB bootstrapping.
    67 	 * Whether to suppress errors during the DB bootstrapping. Default false.
    66 	 *
    68 	 *
    67 	 * @since 2.5.0
    69 	 * @since 2.5.0
    68 	 * @var bool
    70 	 * @var bool
    69 	 */
    71 	 */
    70 	var $suppress_errors = false;
    72 	var $suppress_errors = false;
    71 
    73 
    72 	/**
    74 	/**
    73 	 * The last error during query.
    75 	 * The error encountered during the last query.
    74 	 *
    76 	 *
    75 	 * @since 2.5.0
    77 	 * @since 2.5.0
    76 	 * @var string
    78 	 * @var string
    77 	 */
    79 	 */
    78 	public $last_error = '';
    80 	public $last_error = '';
    79 
    81 
    80 	/**
    82 	/**
    81 	 * Amount of queries made
    83 	 * The number of queries made.
    82 	 *
    84 	 *
    83 	 * @since 1.2.0
    85 	 * @since 1.2.0
    84 	 * @var int
    86 	 * @var int
    85 	 */
    87 	 */
    86 	public $num_queries = 0;
    88 	public $num_queries = 0;
    87 
    89 
    88 	/**
    90 	/**
    89 	 * Count of rows returned by previous query
    91 	 * Count of rows returned by the last query.
    90 	 *
    92 	 *
    91 	 * @since 0.71
    93 	 * @since 0.71
    92 	 * @var int
    94 	 * @var int
    93 	 */
    95 	 */
    94 	public $num_rows = 0;
    96 	public $num_rows = 0;
    95 
    97 
    96 	/**
    98 	/**
    97 	 * Count of affected rows by previous query
    99 	 * Count of rows affected by the last query.
    98 	 *
   100 	 *
    99 	 * @since 0.71
   101 	 * @since 0.71
   100 	 * @var int
   102 	 * @var int
   101 	 */
   103 	 */
   102 	var $rows_affected = 0;
   104 	var $rows_affected = 0;
   103 
   105 
   104 	/**
   106 	/**
   105 	 * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
   107 	 * The ID generated for an AUTO_INCREMENT column by the last query (usually INSERT).
   106 	 *
   108 	 *
   107 	 * @since 0.71
   109 	 * @since 0.71
   108 	 * @var int
   110 	 * @var int
   109 	 */
   111 	 */
   110 	public $insert_id = 0;
   112 	public $insert_id = 0;
   111 
   113 
   112 	/**
   114 	/**
   113 	 * Last query made
   115 	 * The last query made.
   114 	 *
   116 	 *
   115 	 * @since 0.71
   117 	 * @since 0.71
   116 	 * @var array
   118 	 * @var string
   117 	 */
   119 	 */
   118 	var $last_query;
   120 	var $last_query;
   119 
   121 
   120 	/**
   122 	/**
   121 	 * Results of the last query made
   123 	 * Results of the last query.
   122 	 *
   124 	 *
   123 	 * @since 0.71
   125 	 * @since 0.71
   124 	 * @var array|null
   126 	 * @var array|null
   125 	 */
   127 	 */
   126 	var $last_result;
   128 	var $last_result;
   132 	 * @var mixed
   134 	 * @var mixed
   133 	 */
   135 	 */
   134 	protected $result;
   136 	protected $result;
   135 
   137 
   136 	/**
   138 	/**
   137 	 * Cached column info, for sanity checking data before inserting
   139 	 * Cached column info, for sanity checking data before inserting.
   138 	 *
   140 	 *
   139 	 * @since 4.2.0
   141 	 * @since 4.2.0
   140 	 * @var array
   142 	 * @var array
   141 	 */
   143 	 */
   142 	protected $col_meta = array();
   144 	protected $col_meta = array();
   143 
   145 
   144 	/**
   146 	/**
   145 	 * Calculated character sets on tables
   147 	 * Calculated character sets on tables.
   146 	 *
   148 	 *
   147 	 * @since 4.2.0
   149 	 * @since 4.2.0
   148 	 * @var array
   150 	 * @var array
   149 	 */
   151 	 */
   150 	protected $table_charset = array();
   152 	protected $table_charset = array();
   151 
   153 
   152 	/**
   154 	/**
   153 	 * Whether text fields in the current query need to be sanity checked.
   155 	 * Whether text fields in the current query need to be sanity checked. Default false.
   154 	 *
   156 	 *
   155 	 * @since 4.2.0
   157 	 * @since 4.2.0
   156 	 * @var bool
   158 	 * @var bool
   157 	 */
   159 	 */
   158 	protected $check_current_query = true;
   160 	protected $check_current_query = true;
   165 	 * @var bool
   167 	 * @var bool
   166 	 */
   168 	 */
   167 	private $checking_collation = false;
   169 	private $checking_collation = false;
   168 
   170 
   169 	/**
   171 	/**
   170 	 * Saved info on the table column
   172 	 * Saved info on the table column.
   171 	 *
   173 	 *
   172 	 * @since 0.71
   174 	 * @since 0.71
   173 	 * @var array
   175 	 * @var array
   174 	 */
   176 	 */
   175 	protected $col_info;
   177 	protected $col_info;
   178 	 * Log of queries that were executed, for debugging purposes.
   180 	 * Log of queries that were executed, for debugging purposes.
   179 	 *
   181 	 *
   180 	 * @since 1.5.0
   182 	 * @since 1.5.0
   181 	 * @since 2.5.0 The third element in each query log was added to record the calling functions.
   183 	 * @since 2.5.0 The third element in each query log was added to record the calling functions.
   182 	 * @since 5.1.0 The fourth element in each query log was added to record the start time.
   184 	 * @since 5.1.0 The fourth element in each query log was added to record the start time.
       
   185 	 * @since 5.3.0 The fifth element in each query log was added to record custom data.
   183 	 *
   186 	 *
   184 	 * @var array[] {
   187 	 * @var array[] {
   185 	 *     Array of queries that were executed.
   188 	 *     Array of queries that were executed.
   186 	 *
   189 	 *
   187 	 *     @type array ...$0 {
   190 	 *     @type array ...$0 {
   188 	 *         Data for each query.
   191 	 *         Data for each query.
   189 	 *
   192 	 *
   190 	 *         @type string $0 The query's SQL.
   193 	 *         @type string $0 The query's SQL.
   191 	 *         @type float  $1 Total time spent on the query, in seconds.
   194 	 *         @type float  $1 Total time spent on the query, in seconds.
   192 	 *         @type string $2 Comma separated list of the calling functions.
   195 	 *         @type string $2 Comma-separated list of the calling functions.
   193 	 *         @type float  $3 Unix timestamp of the time at the start of the query.
   196 	 *         @type float  $3 Unix timestamp of the time at the start of the query.
       
   197 	 *         @type array  $4 Custom query data.
   194 	 *     }
   198 	 *     }
   195 	 * }
   199 	 * }
   196 	 */
   200 	 */
   197 	var $queries;
   201 	var $queries;
   198 
   202 
   199 	/**
   203 	/**
   200 	 * The number of times to retry reconnecting before dying.
   204 	 * The number of times to retry reconnecting before dying. Default 5.
   201 	 *
   205 	 *
   202 	 * @since 3.9.0
   206 	 * @since 3.9.0
   203 	 * @see wpdb::check_connection()
   207 	 * @see wpdb::check_connection()
   204 	 * @var int
   208 	 * @var int
   205 	 */
   209 	 */
   206 	protected $reconnect_retries = 5;
   210 	protected $reconnect_retries = 5;
   207 
   211 
   208 	/**
   212 	/**
   209 	 * WordPress table prefix
   213 	 * WordPress table prefix
   210 	 *
   214 	 *
   211 	 * You can set this to have multiple WordPress installations
   215 	 * You can set this to have multiple WordPress installations in a single database.
   212 	 * in a single database. The second reason is for possible
   216 	 * The second reason is for possible security precautions.
   213 	 * security precautions.
       
   214 	 *
   217 	 *
   215 	 * @since 2.5.0
   218 	 * @since 2.5.0
   216 	 * @var string
   219 	 * @var string
   217 	 */
   220 	 */
   218 	public $prefix = '';
   221 	public $prefix = '';
   248 	 * @var int
   251 	 * @var int
   249 	 */
   252 	 */
   250 	public $siteid = 0;
   253 	public $siteid = 0;
   251 
   254 
   252 	/**
   255 	/**
   253 	 * List of WordPress per-blog tables
   256 	 * List of WordPress per-blog tables.
   254 	 *
   257 	 *
   255 	 * @since 2.5.0
   258 	 * @since 2.5.0
   256 	 * @see wpdb::tables()
   259 	 * @see wpdb::tables()
   257 	 * @var array
   260 	 * @var array
   258 	 */
   261 	 */
   268 		'termmeta',
   271 		'termmeta',
   269 		'commentmeta',
   272 		'commentmeta',
   270 	);
   273 	);
   271 
   274 
   272 	/**
   275 	/**
   273 	 * List of deprecated WordPress tables
   276 	 * List of deprecated WordPress tables.
   274 	 *
   277 	 *
   275 	 * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
   278 	 * 'categories', 'post2cat', and 'link2cat' were deprecated in 2.3.0, db version 5539.
   276 	 *
   279 	 *
   277 	 * @since 2.9.0
   280 	 * @since 2.9.0
   278 	 * @see wpdb::tables()
   281 	 * @see wpdb::tables()
   279 	 * @var array
   282 	 * @var array
   280 	 */
   283 	 */
   281 	var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
   284 	var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
   282 
   285 
   283 	/**
   286 	/**
   284 	 * List of WordPress global tables
   287 	 * List of WordPress global tables.
   285 	 *
   288 	 *
   286 	 * @since 3.0.0
   289 	 * @since 3.0.0
   287 	 * @see wpdb::tables()
   290 	 * @see wpdb::tables()
   288 	 * @var array
   291 	 * @var array
   289 	 */
   292 	 */
   290 	var $global_tables = array( 'users', 'usermeta' );
   293 	var $global_tables = array( 'users', 'usermeta' );
   291 
   294 
   292 	/**
   295 	/**
   293 	 * List of Multisite global tables
   296 	 * List of Multisite global tables.
   294 	 *
   297 	 *
   295 	 * @since 3.0.0
   298 	 * @since 3.0.0
   296 	 * @see wpdb::tables()
   299 	 * @see wpdb::tables()
   297 	 * @var array
   300 	 * @var array
   298 	 */
   301 	 */
   302 		'signups',
   305 		'signups',
   303 		'site',
   306 		'site',
   304 		'sitemeta',
   307 		'sitemeta',
   305 		'sitecategories',
   308 		'sitecategories',
   306 		'registration_log',
   309 		'registration_log',
   307 		'blog_versions',
       
   308 	);
   310 	);
   309 
   311 
   310 	/**
   312 	/**
   311 	 * WordPress Comments table
   313 	 * WordPress Comments table.
   312 	 *
   314 	 *
   313 	 * @since 1.5.0
   315 	 * @since 1.5.0
   314 	 * @var string
   316 	 * @var string
   315 	 */
   317 	 */
   316 	public $comments;
   318 	public $comments;
   317 
   319 
   318 	/**
   320 	/**
   319 	 * WordPress Comment Metadata table
   321 	 * WordPress Comment Metadata table.
   320 	 *
   322 	 *
   321 	 * @since 2.9.0
   323 	 * @since 2.9.0
   322 	 * @var string
   324 	 * @var string
   323 	 */
   325 	 */
   324 	public $commentmeta;
   326 	public $commentmeta;
   325 
   327 
   326 	/**
   328 	/**
   327 	 * WordPress Links table
   329 	 * WordPress Links table.
   328 	 *
   330 	 *
   329 	 * @since 1.5.0
   331 	 * @since 1.5.0
   330 	 * @var string
   332 	 * @var string
   331 	 */
   333 	 */
   332 	public $links;
   334 	public $links;
   333 
   335 
   334 	/**
   336 	/**
   335 	 * WordPress Options table
   337 	 * WordPress Options table.
   336 	 *
   338 	 *
   337 	 * @since 1.5.0
   339 	 * @since 1.5.0
   338 	 * @var string
   340 	 * @var string
   339 	 */
   341 	 */
   340 	public $options;
   342 	public $options;
   341 
   343 
   342 	/**
   344 	/**
   343 	 * WordPress Post Metadata table
   345 	 * WordPress Post Metadata table.
   344 	 *
   346 	 *
   345 	 * @since 1.5.0
   347 	 * @since 1.5.0
   346 	 * @var string
   348 	 * @var string
   347 	 */
   349 	 */
   348 	public $postmeta;
   350 	public $postmeta;
   349 
   351 
   350 	/**
   352 	/**
   351 	 * WordPress Posts table
   353 	 * WordPress Posts table.
   352 	 *
   354 	 *
   353 	 * @since 1.5.0
   355 	 * @since 1.5.0
   354 	 * @var string
   356 	 * @var string
   355 	 */
   357 	 */
   356 	public $posts;
   358 	public $posts;
   357 
   359 
   358 	/**
   360 	/**
   359 	 * WordPress Terms table
   361 	 * WordPress Terms table.
   360 	 *
   362 	 *
   361 	 * @since 2.3.0
   363 	 * @since 2.3.0
   362 	 * @var string
   364 	 * @var string
   363 	 */
   365 	 */
   364 	public $terms;
   366 	public $terms;
   365 
   367 
   366 	/**
   368 	/**
   367 	 * WordPress Term Relationships table
   369 	 * WordPress Term Relationships table.
   368 	 *
   370 	 *
   369 	 * @since 2.3.0
   371 	 * @since 2.3.0
   370 	 * @var string
   372 	 * @var string
   371 	 */
   373 	 */
   372 	public $term_relationships;
   374 	public $term_relationships;
   373 
   375 
   374 	/**
   376 	/**
   375 	 * WordPress Term Taxonomy table
   377 	 * WordPress Term Taxonomy table.
   376 	 *
   378 	 *
   377 	 * @since 2.3.0
   379 	 * @since 2.3.0
   378 	 * @var string
   380 	 * @var string
   379 	 */
   381 	 */
   380 	public $term_taxonomy;
   382 	public $term_taxonomy;
   390 	//
   392 	//
   391 	// Global and Multisite tables
   393 	// Global and Multisite tables
   392 	//
   394 	//
   393 
   395 
   394 	/**
   396 	/**
   395 	 * WordPress User Metadata table
   397 	 * WordPress User Metadata table.
   396 	 *
   398 	 *
   397 	 * @since 2.3.0
   399 	 * @since 2.3.0
   398 	 * @var string
   400 	 * @var string
   399 	 */
   401 	 */
   400 	public $usermeta;
   402 	public $usermeta;
   401 
   403 
   402 	/**
   404 	/**
   403 	 * WordPress Users table
   405 	 * WordPress Users table.
   404 	 *
   406 	 *
   405 	 * @since 1.5.0
   407 	 * @since 1.5.0
   406 	 * @var string
   408 	 * @var string
   407 	 */
   409 	 */
   408 	public $users;
   410 	public $users;
   409 
   411 
   410 	/**
   412 	/**
   411 	 * Multisite Blogs table
   413 	 * Multisite Blogs table.
   412 	 *
   414 	 *
   413 	 * @since 3.0.0
   415 	 * @since 3.0.0
   414 	 * @var string
   416 	 * @var string
   415 	 */
   417 	 */
   416 	public $blogs;
   418 	public $blogs;
   417 
   419 
   418 	/**
   420 	/**
   419 	 * Multisite Blog Metadata table
   421 	 * Multisite Blog Metadata table.
   420 	 *
   422 	 *
   421 	 * @since 5.1.0
   423 	 * @since 5.1.0
   422 	 * @var string
   424 	 * @var string
   423 	 */
   425 	 */
   424 	public $blogmeta;
   426 	public $blogmeta;
   425 
   427 
   426 	/**
   428 	/**
   427 	 * Multisite Blog Versions table
   429 	 * Multisite Registration Log table.
   428 	 *
   430 	 *
   429 	 * @since 3.0.0
   431 	 * @since 3.0.0
   430 	 * @var string
   432 	 * @var string
   431 	 */
   433 	 */
   432 	public $blog_versions;
   434 	public $registration_log;
   433 
   435 
   434 	/**
   436 	/**
   435 	 * Multisite Registration Log table
   437 	 * Multisite Signups table.
   436 	 *
   438 	 *
   437 	 * @since 3.0.0
   439 	 * @since 3.0.0
   438 	 * @var string
   440 	 * @var string
   439 	 */
   441 	 */
   440 	public $registration_log;
   442 	public $signups;
   441 
   443 
   442 	/**
   444 	/**
   443 	 * Multisite Signups table
   445 	 * Multisite Sites table.
   444 	 *
   446 	 *
   445 	 * @since 3.0.0
   447 	 * @since 3.0.0
   446 	 * @var string
   448 	 * @var string
   447 	 */
   449 	 */
   448 	public $signups;
   450 	public $site;
   449 
   451 
   450 	/**
   452 	/**
   451 	 * Multisite Sites table
   453 	 * Multisite Sitewide Terms table.
   452 	 *
   454 	 *
   453 	 * @since 3.0.0
   455 	 * @since 3.0.0
   454 	 * @var string
   456 	 * @var string
   455 	 */
   457 	 */
   456 	public $site;
   458 	public $sitecategories;
   457 
   459 
   458 	/**
   460 	/**
   459 	 * Multisite Sitewide Terms table
   461 	 * Multisite Site Metadata table.
   460 	 *
   462 	 *
   461 	 * @since 3.0.0
   463 	 * @since 3.0.0
   462 	 * @var string
   464 	 * @var string
   463 	 */
   465 	 */
   464 	public $sitecategories;
       
   465 
       
   466 	/**
       
   467 	 * Multisite Site Metadata table
       
   468 	 *
       
   469 	 * @since 3.0.0
       
   470 	 * @var string
       
   471 	 */
       
   472 	public $sitemeta;
   466 	public $sitemeta;
   473 
   467 
   474 	/**
   468 	/**
   475 	 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
   469 	 * Format specifiers for DB columns.
   476 	 *
   470 	 *
   477 	 * Keys are column names, values are format types: 'ID' => '%d'
   471 	 * Columns not listed here default to %s. Initialized during WP load.
       
   472 	 * Keys are column names, values are format types: 'ID' => '%d'.
   478 	 *
   473 	 *
   479 	 * @since 2.8.0
   474 	 * @since 2.8.0
   480 	 * @see wpdb::prepare()
   475 	 * @see wpdb::prepare()
   481 	 * @see wpdb::insert()
   476 	 * @see wpdb::insert()
   482 	 * @see wpdb::update()
   477 	 * @see wpdb::update()
   485 	 * @var array
   480 	 * @var array
   486 	 */
   481 	 */
   487 	public $field_types = array();
   482 	public $field_types = array();
   488 
   483 
   489 	/**
   484 	/**
   490 	 * Database table columns charset
   485 	 * Database table columns charset.
   491 	 *
   486 	 *
   492 	 * @since 2.2.0
   487 	 * @since 2.2.0
   493 	 * @var string
   488 	 * @var string
   494 	 */
   489 	 */
   495 	public $charset;
   490 	public $charset;
   496 
   491 
   497 	/**
   492 	/**
   498 	 * Database table columns collate
   493 	 * Database table columns collate.
   499 	 *
   494 	 *
   500 	 * @since 2.2.0
   495 	 * @since 2.2.0
   501 	 * @var string
   496 	 * @var string
   502 	 */
   497 	 */
   503 	public $collate;
   498 	public $collate;
   504 
   499 
   505 	/**
   500 	/**
   506 	 * Database Username
   501 	 * Database Username.
   507 	 *
   502 	 *
   508 	 * @since 2.9.0
   503 	 * @since 2.9.0
   509 	 * @var string
   504 	 * @var string
   510 	 */
   505 	 */
   511 	protected $dbuser;
   506 	protected $dbuser;
   512 
   507 
   513 	/**
   508 	/**
   514 	 * Database Password
   509 	 * Database Password.
   515 	 *
   510 	 *
   516 	 * @since 3.1.0
   511 	 * @since 3.1.0
   517 	 * @var string
   512 	 * @var string
   518 	 */
   513 	 */
   519 	protected $dbpassword;
   514 	protected $dbpassword;
   520 
   515 
   521 	/**
   516 	/**
   522 	 * Database Name
   517 	 * Database Name.
   523 	 *
   518 	 *
   524 	 * @since 3.1.0
   519 	 * @since 3.1.0
   525 	 * @var string
   520 	 * @var string
   526 	 */
   521 	 */
   527 	protected $dbname;
   522 	protected $dbname;
   528 
   523 
   529 	/**
   524 	/**
   530 	 * Database Host
   525 	 * Database Host.
   531 	 *
   526 	 *
   532 	 * @since 3.1.0
   527 	 * @since 3.1.0
   533 	 * @var string
   528 	 * @var string
   534 	 */
   529 	 */
   535 	protected $dbhost;
   530 	protected $dbhost;
   536 
   531 
   537 	/**
   532 	/**
   538 	 * Database Handle
   533 	 * Database Handle.
   539 	 *
   534 	 *
   540 	 * @since 0.71
   535 	 * @since 0.71
   541 	 * @var string
   536 	 * @var string
   542 	 */
   537 	 */
   543 	protected $dbh;
   538 	protected $dbh;
   544 
   539 
   545 	/**
   540 	/**
   546 	 * A textual description of the last query/get_row/get_var call
   541 	 * A textual description of the last query/get_row/get_var call.
   547 	 *
   542 	 *
   548 	 * @since 3.0.0
   543 	 * @since 3.0.0
   549 	 * @var string
   544 	 * @var string
   550 	 */
   545 	 */
   551 	public $func_call;
   546 	public $func_call;
   552 
   547 
   553 	/**
   548 	/**
   554 	 * Whether MySQL is used as the database engine.
   549 	 * Whether MySQL is used as the database engine.
   555 	 *
   550 	 *
   556 	 * Set in WPDB::db_connect() to true, by default. This is used when checking
   551 	 * Set in wpdb::db_connect() to true, by default. This is used when checking
   557 	 * against the required MySQL version for WordPress. Normally, a replacement
   552 	 * against the required MySQL version for WordPress. Normally, a replacement
   558 	 * database drop-in (db.php) will skip these checks, but setting this to true
   553 	 * database drop-in (db.php) will skip these checks, but setting this to true
   559 	 * will force the checks to occur.
   554 	 * will force the checks to occur.
   560 	 *
   555 	 *
   561 	 * @since 3.3.0
   556 	 * @since 3.3.0
   573 		'NO_ZERO_DATE',
   568 		'NO_ZERO_DATE',
   574 		'ONLY_FULL_GROUP_BY',
   569 		'ONLY_FULL_GROUP_BY',
   575 		'STRICT_TRANS_TABLES',
   570 		'STRICT_TRANS_TABLES',
   576 		'STRICT_ALL_TABLES',
   571 		'STRICT_ALL_TABLES',
   577 		'TRADITIONAL',
   572 		'TRADITIONAL',
       
   573 		'ANSI',
   578 	);
   574 	);
   579 
   575 
   580 	/**
   576 	/**
   581 	 * Whether to use mysqli over mysql.
   577 	 * Whether to use mysqli over mysql. Default false.
   582 	 *
   578 	 *
   583 	 * @since 3.9.0
   579 	 * @since 3.9.0
   584 	 * @var bool
   580 	 * @var bool
   585 	 */
   581 	 */
   586 	private $use_mysqli = false;
   582 	private $use_mysqli = false;
   587 
   583 
   588 	/**
   584 	/**
   589 	 * Whether we've managed to successfully connect at some point
   585 	 * Whether we've managed to successfully connect at some point.
   590 	 *
   586 	 *
   591 	 * @since 3.9.0
   587 	 * @since 3.9.0
   592 	 * @var bool
   588 	 * @var bool
   593 	 */
   589 	 */
   594 	private $has_connected = false;
   590 	private $has_connected = false;
   595 
   591 
   596 	/**
   592 	/**
   597 	 * Connects to the database server and selects a database
   593 	 * Connects to the database server and selects a database.
   598 	 *
   594 	 *
   599 	 * PHP5 style constructor for compatibility with PHP5. Does
   595 	 * PHP5 style constructor for compatibility with PHP5. Does the actual setting up
   600 	 * the actual setting up of the class properties and connection
   596 	 * of the class properties and connection to the database.
   601 	 * to the database.
   597 	 *
       
   598 	 * @since 2.0.8
   602 	 *
   599 	 *
   603 	 * @link https://core.trac.wordpress.org/ticket/3354
   600 	 * @link https://core.trac.wordpress.org/ticket/3354
   604 	 * @since 2.0.8
   601 	 * @global string $wp_version The WordPress version string.
   605 	 *
   602 	 *
   606 	 * @global string $wp_version
   603 	 * @param string $dbuser     MySQL database user.
   607 	 *
   604 	 * @param string $dbpassword MySQL database password.
   608 	 * @param string $dbuser     MySQL database user
   605 	 * @param string $dbname     MySQL database name.
   609 	 * @param string $dbpassword MySQL database password
   606 	 * @param string $dbhost     MySQL database host.
   610 	 * @param string $dbname     MySQL database name
       
   611 	 * @param string $dbhost     MySQL database host
       
   612 	 */
   607 	 */
   613 	public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
   608 	public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
   614 		register_shutdown_function( array( $this, '__destruct' ) );
       
   615 
       
   616 		if ( WP_DEBUG && WP_DEBUG_DISPLAY ) {
   609 		if ( WP_DEBUG && WP_DEBUG_DISPLAY ) {
   617 			$this->show_errors();
   610 			$this->show_errors();
   618 		}
   611 		}
   619 
   612 
   620 		// Use ext/mysqli if it exists unless WP_USE_EXT_MYSQL is defined as true
   613 		// Use ext/mysqli if it exists unless WP_USE_EXT_MYSQL is defined as true.
   621 		if ( function_exists( 'mysqli_connect' ) ) {
   614 		if ( function_exists( 'mysqli_connect' ) ) {
   622 			$this->use_mysqli = true;
   615 			$this->use_mysqli = true;
   623 
   616 
   624 			if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
   617 			if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
   625 				$this->use_mysqli = ! WP_USE_EXT_MYSQL;
   618 				$this->use_mysqli = ! WP_USE_EXT_MYSQL;
   638 
   631 
   639 		$this->db_connect();
   632 		$this->db_connect();
   640 	}
   633 	}
   641 
   634 
   642 	/**
   635 	/**
   643 	 * PHP5 style destructor and will run when database object is destroyed.
       
   644 	 *
       
   645 	 * @see wpdb::__construct()
       
   646 	 * @since 2.0.8
       
   647 	 * @return true
       
   648 	 */
       
   649 	public function __destruct() {
       
   650 		return true;
       
   651 	}
       
   652 
       
   653 	/**
       
   654 	 * Makes private properties readable for backward compatibility.
   636 	 * Makes private properties readable for backward compatibility.
   655 	 *
   637 	 *
   656 	 * @since 3.5.0
   638 	 * @since 3.5.0
   657 	 *
   639 	 *
   658 	 * @param string $name The private member to get, and optionally process
   640 	 * @param string $name The private member to get, and optionally process.
   659 	 * @return mixed The private member
   641 	 * @return mixed The private member.
   660 	 */
   642 	 */
   661 	public function __get( $name ) {
   643 	public function __get( $name ) {
   662 		if ( 'col_info' === $name ) {
   644 		if ( 'col_info' === $name ) {
   663 			$this->load_col_info();
   645 			$this->load_col_info();
   664 		}
   646 		}
   669 	/**
   651 	/**
   670 	 * Makes private properties settable for backward compatibility.
   652 	 * Makes private properties settable for backward compatibility.
   671 	 *
   653 	 *
   672 	 * @since 3.5.0
   654 	 * @since 3.5.0
   673 	 *
   655 	 *
   674 	 * @param string $name  The private member to set
   656 	 * @param string $name  The private member to set.
   675 	 * @param mixed  $value The value to set
   657 	 * @param mixed  $value The value to set.
   676 	 */
   658 	 */
   677 	public function __set( $name, $value ) {
   659 	public function __set( $name, $value ) {
   678 		$protected_members = array(
   660 		$protected_members = array(
   679 			'col_meta',
   661 			'col_meta',
   680 			'table_charset',
   662 			'table_charset',
   689 	/**
   671 	/**
   690 	 * Makes private properties check-able for backward compatibility.
   672 	 * Makes private properties check-able for backward compatibility.
   691 	 *
   673 	 *
   692 	 * @since 3.5.0
   674 	 * @since 3.5.0
   693 	 *
   675 	 *
   694 	 * @param string $name  The private member to check
   676 	 * @param string $name The private member to check.
   695 	 *
   677 	 * @return bool If the member is set or not.
   696 	 * @return bool If the member is set or not
       
   697 	 */
   678 	 */
   698 	public function __isset( $name ) {
   679 	public function __isset( $name ) {
   699 		return isset( $this->$name );
   680 		return isset( $this->$name );
   700 	}
   681 	}
   701 
   682 
   709 	public function __unset( $name ) {
   690 	public function __unset( $name ) {
   710 		unset( $this->$name );
   691 		unset( $this->$name );
   711 	}
   692 	}
   712 
   693 
   713 	/**
   694 	/**
   714 	 * Set $this->charset and $this->collate
   695 	 * Sets $this->charset and $this->collate.
   715 	 *
   696 	 *
   716 	 * @since 3.1.0
   697 	 * @since 3.1.0
   717 	 */
   698 	 */
   718 	public function init_charset() {
   699 	public function init_charset() {
   719 		$charset = '';
   700 		$charset = '';
   747 	 *
   728 	 *
   748 	 * @since 4.6.0
   729 	 * @since 4.6.0
   749 	 *
   730 	 *
   750 	 * @param string $charset The character set to check.
   731 	 * @param string $charset The character set to check.
   751 	 * @param string $collate The collation to check.
   732 	 * @param string $collate The collation to check.
   752 	 * @return array The most appropriate character set and collation to use.
   733 	 * @return array {
       
   734 	 *     The most appropriate character set and collation to use.
       
   735 	 *
       
   736 	 *     @type string $charset Character set.
       
   737 	 *     @type string $collate Collation.
       
   738 	 * }
   753 	 */
   739 	 */
   754 	public function determine_charset( $charset, $collate ) {
   740 	public function determine_charset( $charset, $collate ) {
   755 		if ( ( $this->use_mysqli && ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) {
   741 		if ( ( $this->use_mysqli && ! ( $this->dbh instanceof mysqli ) ) || empty( $this->dbh ) ) {
   756 			return compact( 'charset', 'collate' );
   742 			return compact( 'charset', 'collate' );
   757 		}
   743 		}
   785 	/**
   771 	/**
   786 	 * Sets the connection's character set.
   772 	 * Sets the connection's character set.
   787 	 *
   773 	 *
   788 	 * @since 3.1.0
   774 	 * @since 3.1.0
   789 	 *
   775 	 *
   790 	 * @param resource $dbh     The resource given by mysql_connect
   776 	 * @param resource $dbh     The resource given by mysql_connect.
   791 	 * @param string   $charset Optional. The character set. Default null.
   777 	 * @param string   $charset Optional. The character set. Default null.
   792 	 * @param string   $collate Optional. The collation. Default null.
   778 	 * @param string   $collate Optional. The collation. Default null.
   793 	 */
   779 	 */
   794 	public function set_charset( $dbh, $charset = null, $collate = null ) {
   780 	public function set_charset( $dbh, $charset = null, $collate = null ) {
   795 		if ( ! isset( $charset ) ) {
   781 		if ( ! isset( $charset ) ) {
   827 			}
   813 			}
   828 		}
   814 		}
   829 	}
   815 	}
   830 
   816 
   831 	/**
   817 	/**
   832 	 * Change the current SQL mode, and ensure its WordPress compatibility.
   818 	 * Changes the current SQL mode, and ensures its WordPress compatibility.
   833 	 *
   819 	 *
   834 	 * If no modes are passed, it will ensure the current MySQL server
   820 	 * If no modes are passed, it will ensure the current MySQL server modes are compatible.
   835 	 * modes are compatible.
       
   836 	 *
   821 	 *
   837 	 * @since 3.9.0
   822 	 * @since 3.9.0
   838 	 *
   823 	 *
   839 	 * @param array $modes Optional. A list of SQL modes to set.
   824 	 * @param array $modes Optional. A list of SQL modes to set. Default empty array.
   840 	 */
   825 	 */
   841 	public function set_sql_mode( $modes = array() ) {
   826 	public function set_sql_mode( $modes = array() ) {
   842 		if ( empty( $modes ) ) {
   827 		if ( empty( $modes ) ) {
   843 			if ( $this->use_mysqli ) {
   828 			if ( $this->use_mysqli ) {
   844 				$res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
   829 				$res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
   877 		 * @param array $incompatible_modes An array of incompatible modes.
   862 		 * @param array $incompatible_modes An array of incompatible modes.
   878 		 */
   863 		 */
   879 		$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
   864 		$incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
   880 
   865 
   881 		foreach ( $modes as $i => $mode ) {
   866 		foreach ( $modes as $i => $mode ) {
   882 			if ( in_array( $mode, $incompatible_modes ) ) {
   867 			if ( in_array( $mode, $incompatible_modes, true ) ) {
   883 				unset( $modes[ $i ] );
   868 				unset( $modes[ $i ] );
   884 			}
   869 			}
   885 		}
   870 		}
   886 
   871 
   887 		$modes_str = implode( ',', $modes );
   872 		$modes_str = implode( ',', $modes );
   897 	 * Sets the table prefix for the WordPress tables.
   882 	 * Sets the table prefix for the WordPress tables.
   898 	 *
   883 	 *
   899 	 * @since 2.5.0
   884 	 * @since 2.5.0
   900 	 *
   885 	 *
   901 	 * @param string $prefix          Alphanumeric name for the new prefix.
   886 	 * @param string $prefix          Alphanumeric name for the new prefix.
   902 	 * @param bool   $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not.
   887 	 * @param bool   $set_table_names Optional. Whether the table names, e.g. wpdb::$posts,
   903 	 * @return string|WP_Error Old prefix or WP_Error on error
   888 	 *                                should be updated or not. Default true.
       
   889 	 * @return string|WP_Error Old prefix or WP_Error on error.
   904 	 */
   890 	 */
   905 	public function set_prefix( $prefix, $set_table_names = true ) {
   891 	public function set_prefix( $prefix, $set_table_names = true ) {
   906 
   892 
   907 		if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
   893 		if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
   908 			return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' );
   894 			return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' );
   937 		}
   923 		}
   938 		return $old_prefix;
   924 		return $old_prefix;
   939 	}
   925 	}
   940 
   926 
   941 	/**
   927 	/**
   942 	 * Sets blog id.
   928 	 * Sets blog ID.
   943 	 *
   929 	 *
   944 	 * @since 3.0.0
   930 	 * @since 3.0.0
   945 	 *
   931 	 *
   946 	 * @param int $blog_id
   932 	 * @param int $blog_id
   947 	 * @param int $network_id Optional.
   933 	 * @param int $network_id Optional.
   948 	 * @return int previous blog id
   934 	 * @return int Previous blog ID.
   949 	 */
   935 	 */
   950 	public function set_blog_id( $blog_id, $network_id = 0 ) {
   936 	public function set_blog_id( $blog_id, $network_id = 0 ) {
   951 		if ( ! empty( $network_id ) ) {
   937 		if ( ! empty( $network_id ) ) {
   952 			$this->siteid = $network_id;
   938 			$this->siteid = $network_id;
   953 		}
   939 		}
   970 
   956 
   971 	/**
   957 	/**
   972 	 * Gets blog prefix.
   958 	 * Gets blog prefix.
   973 	 *
   959 	 *
   974 	 * @since 3.0.0
   960 	 * @since 3.0.0
       
   961 	 *
   975 	 * @param int $blog_id Optional.
   962 	 * @param int $blog_id Optional.
   976 	 * @return string Blog prefix.
   963 	 * @return string Blog prefix.
   977 	 */
   964 	 */
   978 	public function get_blog_prefix( $blog_id = null ) {
   965 	public function get_blog_prefix( $blog_id = null ) {
   979 		if ( is_multisite() ) {
   966 		if ( is_multisite() ) {
   980 			if ( null === $blog_id ) {
   967 			if ( null === $blog_id ) {
   981 				$blog_id = $this->blogid;
   968 				$blog_id = $this->blogid;
   982 			}
   969 			}
       
   970 
   983 			$blog_id = (int) $blog_id;
   971 			$blog_id = (int) $blog_id;
   984 			if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) {
   972 
       
   973 			if ( defined( 'MULTISITE' ) && ( 0 === $blog_id || 1 === $blog_id ) ) {
   985 				return $this->base_prefix;
   974 				return $this->base_prefix;
   986 			} else {
   975 			} else {
   987 				return $this->base_prefix . $blog_id . '_';
   976 				return $this->base_prefix . $blog_id . '_';
   988 			}
   977 			}
   989 		} else {
   978 		} else {
   992 	}
   981 	}
   993 
   982 
   994 	/**
   983 	/**
   995 	 * Returns an array of WordPress tables.
   984 	 * Returns an array of WordPress tables.
   996 	 *
   985 	 *
   997 	 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
   986 	 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to override the WordPress users
   998 	 * override the WordPress users and usermeta tables that would otherwise
   987 	 * and usermeta tables that would otherwise be determined by the prefix.
   999 	 * be determined by the prefix.
   988 	 *
  1000 	 *
   989 	 * The $scope argument can take one of the following:
  1001 	 * The scope argument can take one of the following:
       
  1002 	 *
   990 	 *
  1003 	 * 'all' - returns 'all' and 'global' tables. No old tables are returned.
   991 	 * 'all' - returns 'all' and 'global' tables. No old tables are returned.
  1004 	 * 'blog' - returns the blog-level tables for the queried blog.
   992 	 * 'blog' - returns the blog-level tables for the queried blog.
  1005 	 * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite.
   993 	 * 'global' - returns the global tables for the installation, returning multisite tables only on multisite.
  1006 	 * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
   994 	 * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
  1007 	 * 'old' - returns tables which are deprecated.
   995 	 * 'old' - returns tables which are deprecated.
  1008 	 *
   996 	 *
  1009 	 * @since 3.0.0
   997 	 * @since 3.0.0
       
   998 	 *
  1010 	 * @uses wpdb::$tables
   999 	 * @uses wpdb::$tables
  1011 	 * @uses wpdb::$old_tables
  1000 	 * @uses wpdb::$old_tables
  1012 	 * @uses wpdb::$global_tables
  1001 	 * @uses wpdb::$global_tables
  1013 	 * @uses wpdb::$ms_global_tables
  1002 	 * @uses wpdb::$ms_global_tables
  1014 	 *
  1003 	 *
  1015 	 * @param string $scope   Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
  1004 	 * @param string $scope   Optional. Possible values include 'all', 'global', 'ms_global', 'blog',
  1016 	 * @param bool   $prefix  Optional. Whether to include table prefixes. Default true. If blog
  1005 	 *                        or 'old' tables. Default 'all'.
  1017 	 *                        prefix is requested, then the custom users and usermeta tables will be mapped.
  1006 	 * @param bool   $prefix  Optional. Whether to include table prefixes. If blog prefix is requested,
  1018 	 * @param int    $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
  1007 	 *                        then the custom users and usermeta tables will be mapped. Default true.
       
  1008 	 * @param int    $blog_id Optional. The blog_id to prefix. Used only when prefix is requested.
       
  1009 	 *                        Defaults to wpdb::$blogid.
  1019 	 * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
  1010 	 * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
  1020 	 */
  1011 	 */
  1021 	public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
  1012 	public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
  1022 		switch ( $scope ) {
  1013 		switch ( $scope ) {
  1023 			case 'all':
  1014 			case 'all':
  1051 			}
  1042 			}
  1052 			$blog_prefix   = $this->get_blog_prefix( $blog_id );
  1043 			$blog_prefix   = $this->get_blog_prefix( $blog_id );
  1053 			$base_prefix   = $this->base_prefix;
  1044 			$base_prefix   = $this->base_prefix;
  1054 			$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
  1045 			$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
  1055 			foreach ( $tables as $k => $table ) {
  1046 			foreach ( $tables as $k => $table ) {
  1056 				if ( in_array( $table, $global_tables ) ) {
  1047 				if ( in_array( $table, $global_tables, true ) ) {
  1057 					$tables[ $table ] = $base_prefix . $table;
  1048 					$tables[ $table ] = $base_prefix . $table;
  1058 				} else {
  1049 				} else {
  1059 					$tables[ $table ] = $blog_prefix . $table;
  1050 					$tables[ $table ] = $blog_prefix . $table;
  1060 				}
  1051 				}
  1061 				unset( $tables[ $k ] );
  1052 				unset( $tables[ $k ] );
  1074 	}
  1065 	}
  1075 
  1066 
  1076 	/**
  1067 	/**
  1077 	 * Selects a database using the current database connection.
  1068 	 * Selects a database using the current database connection.
  1078 	 *
  1069 	 *
  1079 	 * The database name will be changed based on the current database
  1070 	 * The database name will be changed based on the current database connection.
  1080 	 * connection. On failure, the execution will bail and display an DB error.
  1071 	 * On failure, the execution will bail and display a DB error.
  1081 	 *
  1072 	 *
  1082 	 * @since 0.71
  1073 	 * @since 0.71
  1083 	 *
  1074 	 *
  1084 	 * @param string        $db  MySQL database name
  1075 	 * @param string        $db  MySQL database name.
  1085 	 * @param resource|null $dbh Optional link identifier.
  1076 	 * @param resource|null $dbh Optional link identifier.
  1086 	 */
  1077 	 */
  1087 	public function select( $db, $dbh = null ) {
  1078 	public function select( $db, $dbh = null ) {
  1088 		if ( is_null( $dbh ) ) {
  1079 		if ( is_null( $dbh ) ) {
  1089 			$dbh = $this->dbh;
  1080 			$dbh = $this->dbh;
  1100 				wp_load_translations_early();
  1091 				wp_load_translations_early();
  1101 
  1092 
  1102 				$message = '<h1>' . __( 'Can&#8217;t select database' ) . "</h1>\n";
  1093 				$message = '<h1>' . __( 'Can&#8217;t select database' ) . "</h1>\n";
  1103 
  1094 
  1104 				$message .= '<p>' . sprintf(
  1095 				$message .= '<p>' . sprintf(
  1105 					/* translators: %s: database name */
  1096 					/* translators: %s: Database name. */
  1106 					__( 'We were able to connect to the database server (which means your username and password is okay) but not able to select the %s database.' ),
  1097 					__( 'We were able to connect to the database server (which means your username and password is okay) but not able to select the %s database.' ),
  1107 					'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>'
  1098 					'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>'
  1108 				) . "</p>\n";
  1099 				) . "</p>\n";
  1109 
  1100 
  1110 				$message .= "<ul>\n";
  1101 				$message .= "<ul>\n";
  1111 				$message .= '<li>' . __( 'Are you sure it exists?' ) . "</li>\n";
  1102 				$message .= '<li>' . __( 'Are you sure it exists?' ) . "</li>\n";
  1112 
  1103 
  1113 				$message .= '<li>' . sprintf(
  1104 				$message .= '<li>' . sprintf(
  1114 					/* translators: 1: database user, 2: database name */
  1105 					/* translators: 1: Database user, 2: Database name. */
  1115 					__( 'Does the user %1$s have permission to use the %2$s database?' ),
  1106 					__( 'Does the user %1$s have permission to use the %2$s database?' ),
  1116 					'<code>' . htmlspecialchars( $this->dbuser, ENT_QUOTES ) . '</code>',
  1107 					'<code>' . htmlspecialchars( $this->dbuser, ENT_QUOTES ) . '</code>',
  1117 					'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>'
  1108 					'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>'
  1118 				) . "</li>\n";
  1109 				) . "</li>\n";
  1119 
  1110 
  1120 				$message .= '<li>' . sprintf(
  1111 				$message .= '<li>' . sprintf(
  1121 					/* translators: %s: database name */
  1112 					/* translators: %s: Database name. */
  1122 					__( '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?' ),
  1113 					__( '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?' ),
  1123 					htmlspecialchars( $db, ENT_QUOTES )
  1114 					htmlspecialchars( $db, ENT_QUOTES )
  1124 				) . "</li>\n";
  1115 				) . "</li>\n";
  1125 
  1116 
  1126 				$message .= "</ul>\n";
  1117 				$message .= "</ul>\n";
  1127 
  1118 
  1128 				$message .= '<p>' . sprintf(
  1119 				$message .= '<p>' . sprintf(
  1129 					/* translators: %s: support forums URL */
  1120 					/* translators: %s: Support forums URL. */
  1130 					__( 'If you don&#8217;t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="%s">WordPress Support Forums</a>.' ),
  1121 					__( 'If you don&#8217;t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="%s">WordPress Support Forums</a>.' ),
  1131 					__( 'https://wordpress.org/support/forums/' )
  1122 					__( 'https://wordpress.org/support/forums/' )
  1132 				) . "</p>\n";
  1123 				) . "</p>\n";
  1133 
  1124 
  1134 				$this->bail( $message, 'db_select_fail' );
  1125 				$this->bail( $message, 'db_select_fail' );
  1141 	 *
  1132 	 *
  1142 	 * Use esc_sql() or wpdb::prepare() instead.
  1133 	 * Use esc_sql() or wpdb::prepare() instead.
  1143 	 *
  1134 	 *
  1144 	 * @since 2.8.0
  1135 	 * @since 2.8.0
  1145 	 * @deprecated 3.6.0 Use wpdb::prepare()
  1136 	 * @deprecated 3.6.0 Use wpdb::prepare()
  1146 	 * @see wpdb::prepare
  1137 	 * @see wpdb::prepare()
  1147 	 * @see esc_sql()
  1138 	 * @see esc_sql()
  1148 	 *
  1139 	 *
  1149 	 * @param string $string
  1140 	 * @param string $string
  1150 	 * @return string
  1141 	 * @return string
  1151 	 */
  1142 	 */
  1155 		}
  1146 		}
  1156 		return addslashes( $string );
  1147 		return addslashes( $string );
  1157 	}
  1148 	}
  1158 
  1149 
  1159 	/**
  1150 	/**
  1160 	 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string()
  1151 	 * Real escape, using mysqli_real_escape_string() or mysql_real_escape_string().
       
  1152 	 *
       
  1153 	 * @since 2.8.0
  1161 	 *
  1154 	 *
  1162 	 * @see mysqli_real_escape_string()
  1155 	 * @see mysqli_real_escape_string()
  1163 	 * @see mysql_real_escape_string()
  1156 	 * @see mysql_real_escape_string()
  1164 	 * @since 2.8.0
  1157 	 *
  1165 	 *
  1158 	 * @param string $string String to escape.
  1166 	 * @param  string $string to escape
  1159 	 * @return string Escaped string.
  1167 	 * @return string escaped
       
  1168 	 */
  1160 	 */
  1169 	function _real_escape( $string ) {
  1161 	function _real_escape( $string ) {
  1170 		if ( $this->dbh ) {
  1162 		if ( $this->dbh ) {
  1171 			if ( $this->use_mysqli ) {
  1163 			if ( $this->use_mysqli ) {
  1172 				$escaped = mysqli_real_escape_string( $this->dbh, $string );
  1164 				$escaped = mysqli_real_escape_string( $this->dbh, $string );
  1174 				$escaped = mysql_real_escape_string( $string, $this->dbh );
  1166 				$escaped = mysql_real_escape_string( $string, $this->dbh );
  1175 			}
  1167 			}
  1176 		} else {
  1168 		} else {
  1177 			$class = get_class( $this );
  1169 			$class = get_class( $this );
  1178 			if ( function_exists( '__' ) ) {
  1170 			if ( function_exists( '__' ) ) {
  1179 				/* translators: %s: database access abstraction class, usually wpdb or a class extending wpdb */
  1171 				/* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */
  1180 				_doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' );
  1172 				_doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' );
  1181 			} else {
  1173 			} else {
  1182 				_doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' );
  1174 				_doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' );
  1183 			}
  1175 			}
  1184 			$escaped = addslashes( $string );
  1176 			$escaped = addslashes( $string );
  1186 
  1178 
  1187 		return $this->add_placeholder_escape( $escaped );
  1179 		return $this->add_placeholder_escape( $escaped );
  1188 	}
  1180 	}
  1189 
  1181 
  1190 	/**
  1182 	/**
  1191 	 * Escape data. Works on arrays.
  1183 	 * Escapes data. Works on arrays.
       
  1184 	 *
       
  1185 	 * @since 2.8.0
  1192 	 *
  1186 	 *
  1193 	 * @uses wpdb::_real_escape()
  1187 	 * @uses wpdb::_real_escape()
  1194 	 * @since  2.8.0
  1188 	 *
  1195 	 *
  1189 	 * @param string|array $data Data to escape.
  1196 	 * @param  string|array $data
  1190 	 * @return string|array Escaped data, in the same type as supplied.
  1197 	 * @return string|array escaped
       
  1198 	 */
  1191 	 */
  1199 	public function _escape( $data ) {
  1192 	public function _escape( $data ) {
  1200 		if ( is_array( $data ) ) {
  1193 		if ( is_array( $data ) ) {
  1201 			foreach ( $data as $k => $v ) {
  1194 			foreach ( $data as $k => $v ) {
  1202 				if ( is_array( $v ) ) {
  1195 				if ( is_array( $v ) ) {
  1220 	 * @since 0.71
  1213 	 * @since 0.71
  1221 	 * @deprecated 3.6.0 Use wpdb::prepare()
  1214 	 * @deprecated 3.6.0 Use wpdb::prepare()
  1222 	 * @see wpdb::prepare()
  1215 	 * @see wpdb::prepare()
  1223 	 * @see esc_sql()
  1216 	 * @see esc_sql()
  1224 	 *
  1217 	 *
  1225 	 * @param mixed $data
  1218 	 * @param string|array $data Data to escape.
  1226 	 * @return mixed
  1219 	 * @return string|array Escaped data, in the same type as supplied.
  1227 	 */
  1220 	 */
  1228 	public function escape( $data ) {
  1221 	public function escape( $data ) {
  1229 		if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) {
  1222 		if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) {
  1230 			_deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' );
  1223 			_deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' );
  1231 		}
  1224 		}
  1243 
  1236 
  1244 		return $data;
  1237 		return $data;
  1245 	}
  1238 	}
  1246 
  1239 
  1247 	/**
  1240 	/**
  1248 	 * Escapes content by reference for insertion into the database, for security
  1241 	 * Escapes content by reference for insertion into the database, for security.
  1249 	 *
  1242 	 *
  1250 	 * @uses wpdb::_real_escape()
  1243 	 * @uses wpdb::_real_escape()
  1251 	 *
  1244 	 *
  1252 	 * @since 2.3.0
  1245 	 * @since 2.3.0
  1253 	 *
  1246 	 *
  1254 	 * @param string $string to escape
  1247 	 * @param string $string String to escape.
  1255 	 */
  1248 	 */
  1256 	public function escape_by_ref( &$string ) {
  1249 	public function escape_by_ref( &$string ) {
  1257 		if ( ! is_float( $string ) ) {
  1250 		if ( ! is_float( $string ) ) {
  1258 			$string = $this->_real_escape( $string );
  1251 			$string = $this->_real_escape( $string );
  1259 		}
  1252 		}
  1260 	}
  1253 	}
  1261 
  1254 
  1262 	/**
  1255 	/**
  1263 	 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
  1256 	 * Prepares a SQL query for safe execution.
  1264 	 *
  1257 	 *
  1265 	 * The following placeholders can be used in the query string:
  1258 	 * Uses sprintf()-like syntax. The following placeholders can be used in the query string:
  1266 	 *   %d (integer)
  1259 	 *   %d (integer)
  1267 	 *   %f (float)
  1260 	 *   %f (float)
  1268 	 *   %s (string)
  1261 	 *   %s (string)
  1269 	 *
  1262 	 *
  1270 	 * All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder.
  1263 	 * All placeholders MUST be left unquoted in the query string. A corresponding argument
  1271 	 *
  1264 	 * MUST be passed for each placeholder.
  1272 	 * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes
  1265 	 *
  1273 	 * added by this function, so should be passed with appropriate quotes around them for your usage.
  1266 	 * Note: There is one exception to the above: for compatibility with old behavior,
  1274 	 *
  1267 	 * numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes
  1275 	 * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example,
  1268 	 * added by this function, so should be passed with appropriate quotes around them.
  1276 	 * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these
  1269 	 *
  1277 	 * cannot be inserted directly in the query string. Also see wpdb::esc_like().
  1270 	 * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards
  1278 	 *
  1271 	 * (for example, to use in LIKE syntax) must be passed via a substitution argument containing
  1279 	 * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination
  1272 	 * the complete LIKE string, these cannot be inserted directly in the query string.
  1280 	 * of the two is not supported.
  1273 	 * Also see wpdb::esc_like().
       
  1274 	 *
       
  1275 	 * Arguments may be passed as individual arguments to the method, or as a single array
       
  1276 	 * containing all arguments. A combination of the two is not supported.
  1281 	 *
  1277 	 *
  1282 	 * Examples:
  1278 	 * Examples:
  1283 	 *     $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) );
  1279 	 *     $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) );
  1284 	 *     $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
  1280 	 *     $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
  1285 	 *
  1281 	 *
  1286 	 * @link https://secure.php.net/sprintf Description of syntax.
       
  1287 	 * @since 2.3.0
  1282 	 * @since 2.3.0
  1288 	 *
  1283 	 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  1289 	 * @param string      $query    Query statement with sprintf()-like placeholders
  1284 	 *              by updating the function signature. The second parameter was changed
  1290 	 * @param array|mixed $args     The array of variables to substitute into the query's placeholders if being called with an array of arguments,
  1285 	 *              from `$args` to `...$args`.
  1291 	 *                              or the first variable to substitute into the query's placeholders if being called with individual arguments.
  1286 	 *
  1292 	 * @param mixed       $args,... further variables to substitute into the query's placeholders if being called wih individual arguments.
  1287 	 * @link https://www.php.net/sprintf Description of syntax.
       
  1288 	 *
       
  1289 	 * @param string      $query   Query statement with sprintf()-like placeholders.
       
  1290 	 * @param array|mixed $args    The array of variables to substitute into the query's placeholders
       
  1291 	 *                             if being called with an array of arguments, or the first variable
       
  1292 	 *                             to substitute into the query's placeholders if being called with
       
  1293 	 *                             individual arguments.
       
  1294 	 * @param mixed       ...$args Further variables to substitute into the query's placeholders
       
  1295 	 *                             if being called with individual arguments.
  1293 	 * @return string|void Sanitized query string, if there is a query to prepare.
  1296 	 * @return string|void Sanitized query string, if there is a query to prepare.
  1294 	 */
  1297 	 */
  1295 	public function prepare( $query, $args ) {
  1298 	public function prepare( $query, ...$args ) {
  1296 		if ( is_null( $query ) ) {
  1299 		if ( is_null( $query ) ) {
  1297 			return;
  1300 			return;
  1298 		}
  1301 		}
  1299 
  1302 
  1300 		// This is not meant to be foolproof -- but it will catch obviously incorrect usage.
  1303 		// This is not meant to be foolproof -- but it will catch obviously incorrect usage.
  1301 		if ( strpos( $query, '%' ) === false ) {
  1304 		if ( strpos( $query, '%' ) === false ) {
  1302 			wp_load_translations_early();
  1305 			wp_load_translations_early();
  1303 			_doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' );
  1306 			_doing_it_wrong(
  1304 		}
  1307 				'wpdb::prepare',
  1305 
  1308 				sprintf(
  1306 		$args = func_get_args();
  1309 					/* translators: %s: wpdb::prepare() */
  1307 		array_shift( $args );
  1310 					__( 'The query argument of %s must have a placeholder.' ),
       
  1311 					'wpdb::prepare()'
       
  1312 				),
       
  1313 				'3.9.0'
       
  1314 			);
       
  1315 		}
  1308 
  1316 
  1309 		// If args were passed as an array (as in vsprintf), move them up.
  1317 		// If args were passed as an array (as in vsprintf), move them up.
  1310 		$passed_as_array = false;
  1318 		$passed_as_array = false;
  1311 		if ( is_array( $args[0] ) && count( $args ) == 1 ) {
  1319 		if ( is_array( $args[0] ) && count( $args ) === 1 ) {
  1312 			$passed_as_array = true;
  1320 			$passed_as_array = true;
  1313 			$args            = $args[0];
  1321 			$args            = $args[0];
  1314 		}
  1322 		}
  1315 
  1323 
  1316 		foreach ( $args as $arg ) {
  1324 		foreach ( $args as $arg ) {
  1317 			if ( ! is_scalar( $arg ) && ! is_null( $arg ) ) {
  1325 			if ( ! is_scalar( $arg ) && ! is_null( $arg ) ) {
  1318 				wp_load_translations_early();
  1326 				wp_load_translations_early();
  1319 				_doing_it_wrong( 'wpdb::prepare', sprintf( __( 'Unsupported value type (%s).' ), gettype( $arg ) ), '4.8.2' );
  1327 				_doing_it_wrong(
       
  1328 					'wpdb::prepare',
       
  1329 					sprintf(
       
  1330 						/* translators: %s: Value type. */
       
  1331 						__( 'Unsupported value type (%s).' ),
       
  1332 						gettype( $arg )
       
  1333 					),
       
  1334 					'4.8.2'
       
  1335 				);
  1320 			}
  1336 			}
  1321 		}
  1337 		}
  1322 
  1338 
  1323 		/*
  1339 		/*
  1324 		 * Specify the formatting allowed in a placeholder. The following are allowed:
  1340 		 * Specify the formatting allowed in a placeholder. The following are allowed:
  1340 		 */
  1356 		 */
  1341 		$query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes.
  1357 		$query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes.
  1342 		$query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes.
  1358 		$query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes.
  1343 		$query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s.
  1359 		$query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s.
  1344 
  1360 
  1345 		$query = preg_replace( "/(?<!%)(%($allowed_format)?f)/", '%\\2F', $query ); // Force floats to be locale unaware.
  1361 		$query = preg_replace( "/(?<!%)(%($allowed_format)?f)/", '%\\2F', $query ); // Force floats to be locale-unaware.
  1346 
  1362 
  1347 		$query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents.
  1363 		$query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents.
  1348 
  1364 
  1349 		// Count the number of valid placeholders in the query.
  1365 		// Count the number of valid placeholders in the query.
  1350 		$placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches );
  1366 		$placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches );
  1351 
  1367 
  1352 		if ( count( $args ) !== $placeholders ) {
  1368 		if ( count( $args ) !== $placeholders ) {
  1353 			if ( 1 === $placeholders && $passed_as_array ) {
  1369 			if ( 1 === $placeholders && $passed_as_array ) {
  1354 				// If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail.
  1370 				// If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail.
  1355 				wp_load_translations_early();
  1371 				wp_load_translations_early();
  1356 				_doing_it_wrong( 'wpdb::prepare', __( 'The query only expected one placeholder, but an array of multiple placeholders was sent.' ), '4.9.0' );
  1372 				_doing_it_wrong(
       
  1373 					'wpdb::prepare',
       
  1374 					__( 'The query only expected one placeholder, but an array of multiple placeholders was sent.' ),
       
  1375 					'4.9.0'
       
  1376 				);
  1357 
  1377 
  1358 				return;
  1378 				return;
  1359 			} else {
  1379 			} else {
  1360 				/*
  1380 				/*
  1361 				 * If we don't have the right number of placeholders, but they were passed as individual arguments,
  1381 				 * If we don't have the right number of placeholders, but they were passed as individual arguments,
  1362 				 * or we were expecting multiple arguments in an array, throw a warning.
  1382 				 * or we were expecting multiple arguments in an array, throw a warning.
  1363 				 */
  1383 				 */
  1364 				wp_load_translations_early();
  1384 				wp_load_translations_early();
  1365 				_doing_it_wrong(
  1385 				_doing_it_wrong(
  1366 					'wpdb::prepare',
  1386 					'wpdb::prepare',
  1367 					/* translators: 1: number of placeholders, 2: number of arguments passed */
       
  1368 					sprintf(
  1387 					sprintf(
       
  1388 						/* translators: 1: Number of placeholders, 2: Number of arguments passed. */
  1369 						__( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
  1389 						__( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
  1370 						$placeholders,
  1390 						$placeholders,
  1371 						count( $args )
  1391 						count( $args )
  1372 					),
  1392 					),
  1373 					'4.8.3'
  1393 					'4.8.3'
  1374 				);
  1394 				);
  1375 			}
  1395 			}
  1376 		}
  1396 		}
  1377 
  1397 
  1378 		array_walk( $args, array( $this, 'escape_by_ref' ) );
  1398 		array_walk( $args, array( $this, 'escape_by_ref' ) );
  1379 		$query = @vsprintf( $query, $args );
  1399 		$query = vsprintf( $query, $args );
  1380 
  1400 
  1381 		return $this->add_placeholder_escape( $query );
  1401 		return $this->add_placeholder_escape( $query );
  1382 	}
  1402 	}
  1383 
  1403 
  1384 	/**
  1404 	/**
  1385 	 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
  1405 	 * First half of escaping for LIKE special characters % and _ before preparing for MySQL.
  1386 	 *
  1406 	 *
  1387 	 * Use this only before wpdb::prepare() or esc_sql().  Reversing the order is very bad for security.
  1407 	 * Use this only before wpdb::prepare() or esc_sql(). Reversing the order is very bad for security.
  1388 	 *
  1408 	 *
  1389 	 * Example Prepared Statement:
  1409 	 * Example Prepared Statement:
  1390 	 *
  1410 	 *
  1391 	 *     $wild = '%';
  1411 	 *     $wild = '%';
  1392 	 *     $find = 'only 43% of planets';
  1412 	 *     $find = 'only 43% of planets';
  1397 	 *
  1417 	 *
  1398 	 *     $sql  = esc_sql( $wpdb->esc_like( $input ) );
  1418 	 *     $sql  = esc_sql( $wpdb->esc_like( $input ) );
  1399 	 *
  1419 	 *
  1400 	 * @since 4.0.0
  1420 	 * @since 4.0.0
  1401 	 *
  1421 	 *
  1402 	 * @param string $text The raw text to be escaped. The input typed by the user should have no
  1422 	 * @param string $text The raw text to be escaped. The input typed by the user
  1403 	 *                     extra or deleted slashes.
  1423 	 *                     should have no extra or deleted slashes.
  1404 	 * @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare()
  1424 	 * @return string Text in the form of a LIKE phrase. The output is not SQL safe.
  1405 	 *                or real_escape next.
  1425 	 *                Call wpdb::prepare() or wpdb::_real_escape() next.
  1406 	 */
  1426 	 */
  1407 	public function esc_like( $text ) {
  1427 	public function esc_like( $text ) {
  1408 		return addcslashes( $text, '_%\\' );
  1428 		return addcslashes( $text, '_%\\' );
  1409 	}
  1429 	}
  1410 
  1430 
  1411 	/**
  1431 	/**
  1412 	 * Print SQL/DB error.
  1432 	 * Prints SQL/DB error.
  1413 	 *
  1433 	 *
  1414 	 * @since 0.71
  1434 	 * @since 0.71
  1415 	 * @global array $EZSQL_ERROR Stores error information of query and error string
  1435 	 *
  1416 	 *
  1436 	 * @global array $EZSQL_ERROR Stores error information of query and error string.
  1417 	 * @param string $str The error to display
  1437 	 *
  1418 	 * @return false|void False if the showing of errors is disabled.
  1438 	 * @param string $str The error to display.
       
  1439 	 * @return void|false Void if the showing of errors is enabled, false if disabled.
  1419 	 */
  1440 	 */
  1420 	public function print_error( $str = '' ) {
  1441 	public function print_error( $str = '' ) {
  1421 		global $EZSQL_ERROR;
  1442 		global $EZSQL_ERROR;
  1422 
  1443 
  1423 		if ( ! $str ) {
  1444 		if ( ! $str ) {
  1436 			return false;
  1457 			return false;
  1437 		}
  1458 		}
  1438 
  1459 
  1439 		wp_load_translations_early();
  1460 		wp_load_translations_early();
  1440 
  1461 
  1441 		if ( $caller = $this->get_caller() ) {
  1462 		$caller = $this->get_caller();
  1442 			/* translators: 1: Database error message, 2: SQL query, 3: Name of the calling function */
  1463 		if ( $caller ) {
       
  1464 			/* translators: 1: Database error message, 2: SQL query, 3: Name of the calling function. */
  1443 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller );
  1465 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller );
  1444 		} else {
  1466 		} else {
  1445 			/* translators: 1: Database error message, 2: SQL query */
  1467 			/* translators: 1: Database error message, 2: SQL query. */
  1446 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query );
  1468 			$error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query );
  1447 		}
  1469 		}
  1448 
  1470 
  1449 		error_log( $error_str );
  1471 		error_log( $error_str );
  1450 
  1472 
  1451 		// Are we showing errors?
  1473 		// Are we showing errors?
  1452 		if ( ! $this->show_errors ) {
  1474 		if ( ! $this->show_errors ) {
  1453 			return false;
  1475 			return false;
  1454 		}
  1476 		}
  1455 
  1477 
  1456 		// If there is an error then take note of it
  1478 		// If there is an error then take note of it.
  1457 		if ( is_multisite() ) {
  1479 		if ( is_multisite() ) {
  1458 			$msg = sprintf(
  1480 			$msg = sprintf(
  1459 				"%s [%s]\n%s\n",
  1481 				"%s [%s]\n%s\n",
  1460 				__( 'WordPress database error:' ),
  1482 				__( 'WordPress database error:' ),
  1461 				$str,
  1483 				$str,
  1483 
  1505 
  1484 	/**
  1506 	/**
  1485 	 * Enables showing of database errors.
  1507 	 * Enables showing of database errors.
  1486 	 *
  1508 	 *
  1487 	 * This function should be used only to enable showing of errors.
  1509 	 * This function should be used only to enable showing of errors.
  1488 	 * wpdb::hide_errors() should be used instead for hiding of errors. However,
  1510 	 * wpdb::hide_errors() should be used instead for hiding errors.
  1489 	 * this function can be used to enable and disable showing of database
       
  1490 	 * errors.
       
  1491 	 *
  1511 	 *
  1492 	 * @since 0.71
  1512 	 * @since 0.71
       
  1513 	 *
  1493 	 * @see wpdb::hide_errors()
  1514 	 * @see wpdb::hide_errors()
  1494 	 *
  1515 	 *
  1495 	 * @param bool $show Whether to show or hide errors
  1516 	 * @param bool $show Optional. Whether to show errors. Default true.
  1496 	 * @return bool Old value for showing errors.
  1517 	 * @return bool Whether showing of errors was previously active.
  1497 	 */
  1518 	 */
  1498 	public function show_errors( $show = true ) {
  1519 	public function show_errors( $show = true ) {
  1499 		$errors            = $this->show_errors;
  1520 		$errors            = $this->show_errors;
  1500 		$this->show_errors = $show;
  1521 		$this->show_errors = $show;
  1501 		return $errors;
  1522 		return $errors;
  1505 	 * Disables showing of database errors.
  1526 	 * Disables showing of database errors.
  1506 	 *
  1527 	 *
  1507 	 * By default database errors are not shown.
  1528 	 * By default database errors are not shown.
  1508 	 *
  1529 	 *
  1509 	 * @since 0.71
  1530 	 * @since 0.71
       
  1531 	 *
  1510 	 * @see wpdb::show_errors()
  1532 	 * @see wpdb::show_errors()
  1511 	 *
  1533 	 *
  1512 	 * @return bool Whether showing of errors was active
  1534 	 * @return bool Whether showing of errors was previously active.
  1513 	 */
  1535 	 */
  1514 	public function hide_errors() {
  1536 	public function hide_errors() {
  1515 		$show              = $this->show_errors;
  1537 		$show              = $this->show_errors;
  1516 		$this->show_errors = false;
  1538 		$this->show_errors = false;
  1517 		return $show;
  1539 		return $show;
  1518 	}
  1540 	}
  1519 
  1541 
  1520 	/**
  1542 	/**
  1521 	 * Whether to suppress database errors.
  1543 	 * Enables or disables suppressing of database errors.
  1522 	 *
  1544 	 *
  1523 	 * By default database errors are suppressed, with a simple
  1545 	 * By default database errors are suppressed.
  1524 	 * call to this function they can be enabled.
       
  1525 	 *
  1546 	 *
  1526 	 * @since 2.5.0
  1547 	 * @since 2.5.0
       
  1548 	 *
  1527 	 * @see wpdb::hide_errors()
  1549 	 * @see wpdb::hide_errors()
  1528 	 * @param bool $suppress Optional. New value. Defaults to true.
  1550 	 *
  1529 	 * @return bool Old value
  1551 	 * @param bool $suppress Optional. Whether to suppress errors. Default true.
       
  1552 	 * @return bool Whether suppressing of errors was previously active.
  1530 	 */
  1553 	 */
  1531 	public function suppress_errors( $suppress = true ) {
  1554 	public function suppress_errors( $suppress = true ) {
  1532 		$errors                = $this->suppress_errors;
  1555 		$errors                = $this->suppress_errors;
  1533 		$this->suppress_errors = (bool) $suppress;
  1556 		$this->suppress_errors = (bool) $suppress;
  1534 		return $errors;
  1557 		return $errors;
  1535 	}
  1558 	}
  1536 
  1559 
  1537 	/**
  1560 	/**
  1538 	 * Kill cached query results.
  1561 	 * Kills cached query results.
  1539 	 *
  1562 	 *
  1540 	 * @since 0.71
  1563 	 * @since 0.71
  1541 	 */
  1564 	 */
  1542 	public function flush() {
  1565 	public function flush() {
  1543 		$this->last_result   = array();
  1566 		$this->last_result   = array();
  1544 		$this->col_info      = null;
  1567 		$this->col_info      = null;
  1545 		$this->last_query    = null;
  1568 		$this->last_query    = null;
  1546 		$this->rows_affected = $this->num_rows = 0;
  1569 		$this->rows_affected = 0;
       
  1570 		$this->num_rows      = 0;
  1547 		$this->last_error    = '';
  1571 		$this->last_error    = '';
  1548 
  1572 
  1549 		if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
  1573 		if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
  1550 			mysqli_free_result( $this->result );
  1574 			mysqli_free_result( $this->result );
  1551 			$this->result = null;
  1575 			$this->result = null;
  1552 
  1576 
  1553 			// Sanity check before using the handle
  1577 			// Sanity check before using the handle.
  1554 			if ( empty( $this->dbh ) || ! ( $this->dbh instanceof mysqli ) ) {
  1578 			if ( empty( $this->dbh ) || ! ( $this->dbh instanceof mysqli ) ) {
  1555 				return;
  1579 				return;
  1556 			}
  1580 			}
  1557 
  1581 
  1558 			// Clear out any results from a multi-query
  1582 			// Clear out any results from a multi-query.
  1559 			while ( mysqli_more_results( $this->dbh ) ) {
  1583 			while ( mysqli_more_results( $this->dbh ) ) {
  1560 				mysqli_next_result( $this->dbh );
  1584 				mysqli_next_result( $this->dbh );
  1561 			}
  1585 			}
  1562 		} elseif ( is_resource( $this->result ) ) {
  1586 		} elseif ( is_resource( $this->result ) ) {
  1563 			mysql_free_result( $this->result );
  1587 			mysql_free_result( $this->result );
  1564 		}
  1588 		}
  1565 	}
  1589 	}
  1566 
  1590 
  1567 	/**
  1591 	/**
  1568 	 * Connect to and select database.
  1592 	 * Connects to and selects database.
  1569 	 *
  1593 	 *
  1570 	 * If $allow_bail is false, the lack of database connection will need
  1594 	 * If $allow_bail is false, the lack of database connection will need to be handled manually.
  1571 	 * to be handled manually.
       
  1572 	 *
  1595 	 *
  1573 	 * @since 3.0.0
  1596 	 * @since 3.0.0
  1574 	 * @since 3.9.0 $allow_bail parameter added.
  1597 	 * @since 3.9.0 $allow_bail parameter added.
  1575 	 *
  1598 	 *
  1576 	 * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  1599 	 * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  1592 			$host    = $this->dbhost;
  1615 			$host    = $this->dbhost;
  1593 			$port    = null;
  1616 			$port    = null;
  1594 			$socket  = null;
  1617 			$socket  = null;
  1595 			$is_ipv6 = false;
  1618 			$is_ipv6 = false;
  1596 
  1619 
  1597 			if ( $host_data = $this->parse_db_host( $this->dbhost ) ) {
  1620 			$host_data = $this->parse_db_host( $this->dbhost );
       
  1621 			if ( $host_data ) {
  1598 				list( $host, $port, $socket, $is_ipv6 ) = $host_data;
  1622 				list( $host, $port, $socket, $is_ipv6 ) = $host_data;
  1599 			}
  1623 			}
  1600 
  1624 
  1601 			/*
  1625 			/*
  1602 			 * If using the `mysqlnd` library, the IPv6 address needs to be
  1626 			 * If using the `mysqlnd` library, the IPv6 address needs to be enclosed
  1603 			 * enclosed in square brackets, whereas it doesn't while using the
  1627 			 * in square brackets, whereas it doesn't while using the `libmysqlclient` library.
  1604 			 * `libmysqlclient` library.
       
  1605 			 * @see https://bugs.php.net/bug.php?id=67563
  1628 			 * @see https://bugs.php.net/bug.php?id=67563
  1606 			 */
  1629 			 */
  1607 			if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) {
  1630 			if ( $is_ipv6 && extension_loaded( 'mysqlnd' ) ) {
  1608 				$host = "[$host]";
  1631 				$host = "[$host]";
  1609 			}
  1632 			}
  1610 
  1633 
  1611 			if ( WP_DEBUG ) {
  1634 			if ( WP_DEBUG ) {
  1612 				mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
  1635 				mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
  1613 			} else {
  1636 			} else {
       
  1637 				// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  1614 				@mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
  1638 				@mysqli_real_connect( $this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
  1615 			}
  1639 			}
  1616 
  1640 
  1617 			if ( $this->dbh->connect_errno ) {
  1641 			if ( $this->dbh->connect_errno ) {
  1618 				$this->dbh = null;
  1642 				$this->dbh = null;
  1619 
  1643 
  1620 				/*
  1644 				/*
  1621 				 * It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if:
  1645 				 * It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if:
  1622 				  *  - We haven't previously connected, and
  1646 				 *  - We haven't previously connected, and
  1623 				  *  - WP_USE_EXT_MYSQL isn't set to false, and
  1647 				 *  - WP_USE_EXT_MYSQL isn't set to false, and
  1624 				  *  - ext/mysql is loaded.
  1648 				 *  - ext/mysql is loaded.
  1625 				  */
  1649 				 */
  1626 				$attempt_fallback = true;
  1650 				$attempt_fallback = true;
  1627 
  1651 
  1628 				if ( $this->has_connected ) {
  1652 				if ( $this->has_connected ) {
  1629 					$attempt_fallback = false;
  1653 					$attempt_fallback = false;
  1630 				} elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) {
  1654 				} elseif ( defined( 'WP_USE_EXT_MYSQL' ) && ! WP_USE_EXT_MYSQL ) {
  1640 			}
  1664 			}
  1641 		} else {
  1665 		} else {
  1642 			if ( WP_DEBUG ) {
  1666 			if ( WP_DEBUG ) {
  1643 				$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1667 				$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1644 			} else {
  1668 			} else {
       
  1669 				// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  1645 				$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1670 				$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags );
  1646 			}
  1671 			}
  1647 		}
  1672 		}
  1648 
  1673 
  1649 		if ( ! $this->dbh && $allow_bail ) {
  1674 		if ( ! $this->dbh && $allow_bail ) {
  1650 			wp_load_translations_early();
  1675 			wp_load_translations_early();
  1651 
  1676 
  1652 			// Load custom DB error template, if present.
  1677 			// Load custom DB error template, if present.
  1653 			if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
  1678 			if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
  1654 				require_once( WP_CONTENT_DIR . '/db-error.php' );
  1679 				require_once WP_CONTENT_DIR . '/db-error.php';
  1655 				die();
  1680 				die();
  1656 			}
  1681 			}
  1657 
  1682 
  1658 			$message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n";
  1683 			$message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n";
  1659 
  1684 
  1660 			$message .= '<p>' . sprintf(
  1685 			$message .= '<p>' . sprintf(
  1661 				/* translators: 1: wp-config.php, 2: database host */
  1686 				/* translators: 1: wp-config.php, 2: Database host. */
  1662 				__( 'This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.' ),
  1687 				__( 'This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.' ),
  1663 				'<code>wp-config.php</code>',
  1688 				'<code>wp-config.php</code>',
  1664 				'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
  1689 				'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
  1665 			) . "</p>\n";
  1690 			) . "</p>\n";
  1666 
  1691 
  1667 			$message .= "<ul>\n";
  1692 			$message .= "<ul>\n";
  1668 			$message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n";
  1693 			$message .= '<li>' . __( 'Are you sure you have the correct username and password?' ) . "</li>\n";
  1669 			$message .= '<li>' . __( 'Are you sure that you have typed the correct hostname?' ) . "</li>\n";
  1694 			$message .= '<li>' . __( 'Are you sure you have typed the correct hostname?' ) . "</li>\n";
  1670 			$message .= '<li>' . __( 'Are you sure that the database server is running?' ) . "</li>\n";
  1695 			$message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n";
  1671 			$message .= "</ul>\n";
  1696 			$message .= "</ul>\n";
  1672 
  1697 
  1673 			$message .= '<p>' . sprintf(
  1698 			$message .= '<p>' . sprintf(
  1674 				/* translators: %s: support forums URL */
  1699 				/* translators: %s: Support forums URL. */
  1675 				__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
  1700 				__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
  1676 				__( 'https://wordpress.org/support/forums/' )
  1701 				__( 'https://wordpress.org/support/forums/' )
  1677 			) . "</p>\n";
  1702 			) . "</p>\n";
  1678 
  1703 
  1679 			$this->bail( $message, 'db_connect_fail' );
  1704 			$this->bail( $message, 'db_connect_fail' );
  1697 
  1722 
  1698 		return false;
  1723 		return false;
  1699 	}
  1724 	}
  1700 
  1725 
  1701 	/**
  1726 	/**
  1702 	 * Parse the DB_HOST setting to interpret it for mysqli_real_connect.
  1727 	 * Parses the DB_HOST setting to interpret it for mysqli_real_connect().
  1703 	 *
  1728 	 *
  1704 	 * mysqli_real_connect doesn't support the host param including a port or
  1729 	 * mysqli_real_connect() doesn't support the host param including a port or socket
  1705 	 * socket like mysql_connect does. This duplicates how mysql_connect detects
  1730 	 * like mysql_connect() does. This duplicates how mysql_connect() detects a port
  1706 	 * a port and/or socket file.
  1731 	 * and/or socket file.
  1707 	 *
  1732 	 *
  1708 	 * @since 4.9.0
  1733 	 * @since 4.9.0
  1709 	 *
  1734 	 *
  1710 	 * @param string $host The DB_HOST setting to parse.
  1735 	 * @param string $host The DB_HOST setting to parse.
  1711 	 * @return array|bool Array containing the host, the port, the socket and whether
  1736 	 * @return array|false Array containing the host, the port, the socket and
  1712 	 *                    it is an IPv6 address, in that order. If $host couldn't be parsed,
  1737 	 *                     whether it is an IPv6 address, in that order.
  1713 	 *                    returns false.
  1738 	 *                     False if $host couldn't be parsed.
  1714 	 */
  1739 	 */
  1715 	public function parse_db_host( $host ) {
  1740 	public function parse_db_host( $host ) {
  1716 		$port    = null;
  1741 		$port    = null;
  1717 		$socket  = null;
  1742 		$socket  = null;
  1718 		$is_ipv6 = false;
  1743 		$is_ipv6 = false;
  1719 
  1744 
  1720 		// First peel off the socket parameter from the right, if it exists.
  1745 		// First peel off the socket parameter from the right, if it exists.
  1721 		$socket_pos = strpos( $host, ':/' );
  1746 		$socket_pos = strpos( $host, ':/' );
  1722 		if ( $socket_pos !== false ) {
  1747 		if ( false !== $socket_pos ) {
  1723 			$socket = substr( $host, $socket_pos + 1 );
  1748 			$socket = substr( $host, $socket_pos + 1 );
  1724 			$host   = substr( $host, 0, $socket_pos );
  1749 			$host   = substr( $host, 0, $socket_pos );
  1725 		}
  1750 		}
  1726 
  1751 
  1727 		// We need to check for an IPv6 address first.
  1752 		// We need to check for an IPv6 address first.
  1753 	}
  1778 	}
  1754 
  1779 
  1755 	/**
  1780 	/**
  1756 	 * Checks that the connection to the database is still up. If not, try to reconnect.
  1781 	 * Checks that the connection to the database is still up. If not, try to reconnect.
  1757 	 *
  1782 	 *
  1758 	 * If this function is unable to reconnect, it will forcibly die, or if after the
  1783 	 * If this function is unable to reconnect, it will forcibly die, or if called
  1759 	 * the {@see 'template_redirect'} hook has been fired, return false instead.
  1784 	 * after the {@see 'template_redirect'} hook has been fired, return false instead.
  1760 	 *
  1785 	 *
  1761 	 * If $allow_bail is false, the lack of database connection will need
  1786 	 * If $allow_bail is false, the lack of database connection will need to be handled manually.
  1762 	 * to be handled manually.
       
  1763 	 *
  1787 	 *
  1764 	 * @since 3.9.0
  1788 	 * @since 3.9.0
  1765 	 *
  1789 	 *
  1766 	 * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  1790 	 * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  1767 	 * @return bool|void True if the connection is up.
  1791 	 * @return bool|void True if the connection is up.
  1777 			}
  1801 			}
  1778 		}
  1802 		}
  1779 
  1803 
  1780 		$error_reporting = false;
  1804 		$error_reporting = false;
  1781 
  1805 
  1782 		// Disable warnings, as we don't want to see a multitude of "unable to connect" messages
  1806 		// Disable warnings, as we don't want to see a multitude of "unable to connect" messages.
  1783 		if ( WP_DEBUG ) {
  1807 		if ( WP_DEBUG ) {
  1784 			$error_reporting = error_reporting();
  1808 			$error_reporting = error_reporting();
  1785 			error_reporting( $error_reporting & ~E_WARNING );
  1809 			error_reporting( $error_reporting & ~E_WARNING );
  1786 		}
  1810 		}
  1787 
  1811 
  1788 		for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
  1812 		for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {
  1789 			// On the last try, re-enable warnings. We want to see a single instance of the
  1813 			// On the last try, re-enable warnings. We want to see a single instance
  1790 			// "unable to connect" message on the bail() screen, if it appears.
  1814 			// of the "unable to connect" message on the bail() screen, if it appears.
  1791 			if ( $this->reconnect_retries === $tries && WP_DEBUG ) {
  1815 			if ( $this->reconnect_retries === $tries && WP_DEBUG ) {
  1792 				error_reporting( $error_reporting );
  1816 				error_reporting( $error_reporting );
  1793 			}
  1817 			}
  1794 
  1818 
  1795 			if ( $this->db_connect( false ) ) {
  1819 			if ( $this->db_connect( false ) ) {
  1816 		wp_load_translations_early();
  1840 		wp_load_translations_early();
  1817 
  1841 
  1818 		$message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n";
  1842 		$message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n";
  1819 
  1843 
  1820 		$message .= '<p>' . sprintf(
  1844 		$message .= '<p>' . sprintf(
  1821 			/* translators: %s: database host */
  1845 			/* translators: %s: Database host. */
  1822 			__( 'This means that we lost contact with the database server at %s. This could mean your host&#8217;s database server is down.' ),
  1846 			__( 'This means that we lost contact with the database server at %s. This could mean your host&#8217;s database server is down.' ),
  1823 			'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
  1847 			'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
  1824 		) . "</p>\n";
  1848 		) . "</p>\n";
  1825 
  1849 
  1826 		$message .= "<ul>\n";
  1850 		$message .= "<ul>\n";
  1827 		$message .= '<li>' . __( 'Are you sure that the database server is running?' ) . "</li>\n";
  1851 		$message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n";
  1828 		$message .= '<li>' . __( 'Are you sure that the database server is not under particularly heavy load?' ) . "</li>\n";
  1852 		$message .= '<li>' . __( 'Are you sure the database server is not under particularly heavy load?' ) . "</li>\n";
  1829 		$message .= "</ul>\n";
  1853 		$message .= "</ul>\n";
  1830 
  1854 
  1831 		$message .= '<p>' . sprintf(
  1855 		$message .= '<p>' . sprintf(
  1832 			/* translators: %s: support forums URL */
  1856 			/* translators: %s: Support forums URL. */
  1833 			__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
  1857 			__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
  1834 			__( 'https://wordpress.org/support/forums/' )
  1858 			__( 'https://wordpress.org/support/forums/' )
  1835 		) . "</p>\n";
  1859 		) . "</p>\n";
  1836 
  1860 
  1837 		// We weren't able to reconnect, so we better bail.
  1861 		// We weren't able to reconnect, so we better bail.
  1838 		$this->bail( $message, 'db_connect_fail' );
  1862 		$this->bail( $message, 'db_connect_fail' );
  1839 
  1863 
  1840 		// Call dead_db() if bail didn't die, because this database is no more. It has ceased to be (at least temporarily).
  1864 		// Call dead_db() if bail didn't die, because this database is no more.
       
  1865 		// It has ceased to be (at least temporarily).
  1841 		dead_db();
  1866 		dead_db();
  1842 	}
  1867 	}
  1843 
  1868 
  1844 	/**
  1869 	/**
  1845 	 * Perform a MySQL database query, using current database connection.
  1870 	 * Performs a MySQL database query, using current database connection.
  1846 	 *
  1871 	 *
  1847 	 * More information can be found on the codex page.
  1872 	 * More information can be found on the Codex page.
  1848 	 *
  1873 	 *
  1849 	 * @since 0.71
  1874 	 * @since 0.71
  1850 	 *
  1875 	 *
  1851 	 * @param string $query Database query
  1876 	 * @link https://codex.wordpress.org/Function_Reference/wpdb_Class
       
  1877 	 *
       
  1878 	 * @param string $query Database query.
  1852 	 * @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows
  1879 	 * @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows
  1853 	 *                  affected/selected for all other queries. Boolean false on error.
  1880 	 *                  affected/selected for all other queries. Boolean false on error.
  1854 	 */
  1881 	 */
  1855 	public function query( $query ) {
  1882 	public function query( $query ) {
  1856 		if ( ! $this->ready ) {
  1883 		if ( ! $this->ready ) {
  1870 		 */
  1897 		 */
  1871 		$query = apply_filters( 'query', $query );
  1898 		$query = apply_filters( 'query', $query );
  1872 
  1899 
  1873 		$this->flush();
  1900 		$this->flush();
  1874 
  1901 
  1875 		// Log how the function was called
  1902 		// Log how the function was called.
  1876 		$this->func_call = "\$db->query(\"$query\")";
  1903 		$this->func_call = "\$db->query(\"$query\")";
  1877 
  1904 
  1878 		// If we're writing to the database, make sure the query will write safely.
  1905 		// If we're writing to the database, make sure the query will write safely.
  1879 		if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
  1906 		if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
  1880 			$stripped_query = $this->strip_invalid_text_from_query( $query );
  1907 			$stripped_query = $this->strip_invalid_text_from_query( $query );
  1912 					$mysql_errno = 2006;
  1939 					$mysql_errno = 2006;
  1913 				}
  1940 				}
  1914 			}
  1941 			}
  1915 		}
  1942 		}
  1916 
  1943 
  1917 		if ( empty( $this->dbh ) || 2006 == $mysql_errno ) {
  1944 		if ( empty( $this->dbh ) || 2006 === $mysql_errno ) {
  1918 			if ( $this->check_connection() ) {
  1945 			if ( $this->check_connection() ) {
  1919 				$this->_do_query( $query );
  1946 				$this->_do_query( $query );
  1920 			} else {
  1947 			} else {
  1921 				$this->insert_id = 0;
  1948 				$this->insert_id = 0;
  1922 				return false;
  1949 				return false;
  1954 			if ( $this->use_mysqli ) {
  1981 			if ( $this->use_mysqli ) {
  1955 				$this->rows_affected = mysqli_affected_rows( $this->dbh );
  1982 				$this->rows_affected = mysqli_affected_rows( $this->dbh );
  1956 			} else {
  1983 			} else {
  1957 				$this->rows_affected = mysql_affected_rows( $this->dbh );
  1984 				$this->rows_affected = mysql_affected_rows( $this->dbh );
  1958 			}
  1985 			}
  1959 			// Take note of the insert_id
  1986 			// Take note of the insert_id.
  1960 			if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
  1987 			if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
  1961 				if ( $this->use_mysqli ) {
  1988 				if ( $this->use_mysqli ) {
  1962 					$this->insert_id = mysqli_insert_id( $this->dbh );
  1989 					$this->insert_id = mysqli_insert_id( $this->dbh );
  1963 				} else {
  1990 				} else {
  1964 					$this->insert_id = mysql_insert_id( $this->dbh );
  1991 					$this->insert_id = mysql_insert_id( $this->dbh );
  1965 				}
  1992 				}
  1966 			}
  1993 			}
  1967 			// Return number of rows affected
  1994 			// Return number of rows affected.
  1968 			$return_val = $this->rows_affected;
  1995 			$return_val = $this->rows_affected;
  1969 		} else {
  1996 		} else {
  1970 			$num_rows = 0;
  1997 			$num_rows = 0;
  1971 			if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
  1998 			if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
  1972 				while ( $row = mysqli_fetch_object( $this->result ) ) {
  1999 				while ( $row = mysqli_fetch_object( $this->result ) ) {
  1978 					$this->last_result[ $num_rows ] = $row;
  2005 					$this->last_result[ $num_rows ] = $row;
  1979 					$num_rows++;
  2006 					$num_rows++;
  1980 				}
  2007 				}
  1981 			}
  2008 			}
  1982 
  2009 
  1983 			// Log number of rows the query returned
  2010 			// Log and return the number of rows selected.
  1984 			// and return number of rows selected
       
  1985 			$this->num_rows = $num_rows;
  2011 			$this->num_rows = $num_rows;
  1986 			$return_val     = $num_rows;
  2012 			$return_val     = $num_rows;
  1987 		}
  2013 		}
  1988 
  2014 
  1989 		return $return_val;
  2015 		return $return_val;
  2009 			$this->result = mysql_query( $query, $this->dbh );
  2035 			$this->result = mysql_query( $query, $this->dbh );
  2010 		}
  2036 		}
  2011 		$this->num_queries++;
  2037 		$this->num_queries++;
  2012 
  2038 
  2013 		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
  2039 		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
  2014 			$this->queries[] = array(
  2040 			$this->log_query(
  2015 				$query,
  2041 				$query,
  2016 				$this->timer_stop(),
  2042 				$this->timer_stop(),
  2017 				$this->get_caller(),
  2043 				$this->get_caller(),
  2018 				$this->time_start,
  2044 				$this->time_start,
       
  2045 				array()
  2019 			);
  2046 			);
  2020 		}
  2047 		}
       
  2048 	}
       
  2049 
       
  2050 	/**
       
  2051 	 * Logs query data.
       
  2052 	 *
       
  2053 	 * @since 5.3.0
       
  2054 	 *
       
  2055 	 * @param string $query           The query's SQL.
       
  2056 	 * @param float  $query_time      Total time spent on the query, in seconds.
       
  2057 	 * @param string $query_callstack Comma-separated list of the calling functions.
       
  2058 	 * @param float  $query_start     Unix timestamp of the time at the start of the query.
       
  2059 	 * @param array  $query_data      Custom query data.
       
  2060 	 */
       
  2061 	public function log_query( $query, $query_time, $query_callstack, $query_start, $query_data ) {
       
  2062 		/**
       
  2063 		 * Filters the custom query data being logged.
       
  2064 		 *
       
  2065 		 * Caution should be used when modifying any of this data, it is recommended that any additional
       
  2066 		 * information you need to store about a query be added as a new associative entry to the fourth
       
  2067 		 * element $query_data.
       
  2068 		 *
       
  2069 		 * @since 5.3.0
       
  2070 		 *
       
  2071 		 * @param array  $query_data      Custom query data.
       
  2072 		 * @param string $query           The query's SQL.
       
  2073 		 * @param float  $query_time      Total time spent on the query, in seconds.
       
  2074 		 * @param string $query_callstack Comma-separated list of the calling functions.
       
  2075 		 * @param float  $query_start     Unix timestamp of the time at the start of the query.
       
  2076 		 */
       
  2077 		$query_data = apply_filters( 'log_query_custom_data', $query_data, $query, $query_time, $query_callstack, $query_start );
       
  2078 
       
  2079 		$this->queries[] = array(
       
  2080 			$query,
       
  2081 			$query_time,
       
  2082 			$query_callstack,
       
  2083 			$query_start,
       
  2084 			$query_data,
       
  2085 		);
  2021 	}
  2086 	}
  2022 
  2087 
  2023 	/**
  2088 	/**
  2024 	 * Generates and returns a placeholder escape string for use in queries returned by ::prepare().
  2089 	 * Generates and returns a placeholder escape string for use in queries returned by ::prepare().
  2025 	 *
  2090 	 *
  2041 
  2106 
  2042 		/*
  2107 		/*
  2043 		 * Add the filter to remove the placeholder escaper. Uses priority 0, so that anything
  2108 		 * Add the filter to remove the placeholder escaper. Uses priority 0, so that anything
  2044 		 * else attached to this filter will receive the query with the placeholder string removed.
  2109 		 * else attached to this filter will receive the query with the placeholder string removed.
  2045 		 */
  2110 		 */
  2046 		if ( ! has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) {
  2111 		if ( false === has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) {
  2047 			add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 );
  2112 			add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 );
  2048 		}
  2113 		}
  2049 
  2114 
  2050 		return $placeholder;
  2115 		return $placeholder;
  2051 	}
  2116 	}
  2077 	public function remove_placeholder_escape( $query ) {
  2142 	public function remove_placeholder_escape( $query ) {
  2078 		return str_replace( $this->placeholder_escape(), '%', $query );
  2143 		return str_replace( $this->placeholder_escape(), '%', $query );
  2079 	}
  2144 	}
  2080 
  2145 
  2081 	/**
  2146 	/**
  2082 	 * Insert a row into a table.
  2147 	 * Inserts a row into the table.
  2083 	 *
  2148 	 *
       
  2149 	 * Examples:
  2084 	 *     wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  2150 	 *     wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  2085 	 *     wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  2151 	 *     wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  2086 	 *
  2152 	 *
  2087 	 * @since 2.5.0
  2153 	 * @since 2.5.0
       
  2154 	 *
  2088 	 * @see wpdb::prepare()
  2155 	 * @see wpdb::prepare()
  2089 	 * @see wpdb::$field_types
  2156 	 * @see wpdb::$field_types
  2090 	 * @see wp_set_wpdb_vars()
  2157 	 * @see wp_set_wpdb_vars()
  2091 	 *
  2158 	 *
  2092 	 * @param string       $table  Table name
  2159 	 * @param string       $table  Table name.
  2093 	 * @param array        $data   Data to insert (in column => value pairs).
  2160 	 * @param array        $data   Data to insert (in column => value pairs).
  2094 	 *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2161 	 *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2095 	 *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
  2162 	 *                             Sending a null value will cause the column to be set to NULL - the corresponding
       
  2163 	 *                             format is ignored in this case.
  2096 	 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
  2164 	 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
  2097 	 *                             If string, that format will be used for all of the values in $data.
  2165 	 *                             If string, that format will be used for all of the values in $data.
  2098 	 *                             A format is one of '%d', '%f', '%s' (integer, float, string).
  2166 	 *                             A format is one of '%d', '%f', '%s' (integer, float, string).
  2099 	 *                             If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  2167 	 *                             If omitted, all values in $data will be treated as strings unless otherwise
       
  2168 	 *                             specified in wpdb::$field_types.
  2100 	 * @return int|false The number of rows inserted, or false on error.
  2169 	 * @return int|false The number of rows inserted, or false on error.
  2101 	 */
  2170 	 */
  2102 	public function insert( $table, $data, $format = null ) {
  2171 	public function insert( $table, $data, $format = null ) {
  2103 		return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
  2172 		return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
  2104 	}
  2173 	}
  2105 
  2174 
  2106 	/**
  2175 	/**
  2107 	 * Replace a row into a table.
  2176 	 * Replaces a row in the table.
  2108 	 *
  2177 	 *
       
  2178 	 * Examples:
  2109 	 *     wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  2179 	 *     wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  2110 	 *     wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  2180 	 *     wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  2111 	 *
  2181 	 *
  2112 	 * @since 3.0.0
  2182 	 * @since 3.0.0
       
  2183 	 *
  2113 	 * @see wpdb::prepare()
  2184 	 * @see wpdb::prepare()
  2114 	 * @see wpdb::$field_types
  2185 	 * @see wpdb::$field_types
  2115 	 * @see wp_set_wpdb_vars()
  2186 	 * @see wp_set_wpdb_vars()
  2116 	 *
  2187 	 *
  2117 	 * @param string       $table  Table name
  2188 	 * @param string       $table  Table name.
  2118 	 * @param array        $data   Data to insert (in column => value pairs).
  2189 	 * @param array        $data   Data to insert (in column => value pairs).
  2119 	 *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2190 	 *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2120 	 *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
  2191 	 *                             Sending a null value will cause the column to be set to NULL - the corresponding
       
  2192 	 *                             format is ignored in this case.
  2121 	 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
  2193 	 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
  2122 	 *                             If string, that format will be used for all of the values in $data.
  2194 	 *                             If string, that format will be used for all of the values in $data.
  2123 	 *                             A format is one of '%d', '%f', '%s' (integer, float, string).
  2195 	 *                             A format is one of '%d', '%f', '%s' (integer, float, string).
  2124 	 *                             If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  2196 	 *                             If omitted, all values in $data will be treated as strings unless otherwise
       
  2197 	 *                             specified in wpdb::$field_types.
  2125 	 * @return int|false The number of rows affected, or false on error.
  2198 	 * @return int|false The number of rows affected, or false on error.
  2126 	 */
  2199 	 */
  2127 	public function replace( $table, $data, $format = null ) {
  2200 	public function replace( $table, $data, $format = null ) {
  2128 		return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
  2201 		return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
  2129 	}
  2202 	}
  2132 	 * Helper function for insert and replace.
  2205 	 * Helper function for insert and replace.
  2133 	 *
  2206 	 *
  2134 	 * Runs an insert or replace query based on $type argument.
  2207 	 * Runs an insert or replace query based on $type argument.
  2135 	 *
  2208 	 *
  2136 	 * @since 3.0.0
  2209 	 * @since 3.0.0
       
  2210 	 *
  2137 	 * @see wpdb::prepare()
  2211 	 * @see wpdb::prepare()
  2138 	 * @see wpdb::$field_types
  2212 	 * @see wpdb::$field_types
  2139 	 * @see wp_set_wpdb_vars()
  2213 	 * @see wp_set_wpdb_vars()
  2140 	 *
  2214 	 *
  2141 	 * @param string       $table  Table name
  2215 	 * @param string       $table  Table name.
  2142 	 * @param array        $data   Data to insert (in column => value pairs).
  2216 	 * @param array        $data   Data to insert (in column => value pairs).
  2143 	 *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2217 	 *                             Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2144 	 *                             Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
  2218 	 *                             Sending a null value will cause the column to be set to NULL - the corresponding
       
  2219 	 *                             format is ignored in this case.
  2145 	 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
  2220 	 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data.
  2146 	 *                             If string, that format will be used for all of the values in $data.
  2221 	 *                             If string, that format will be used for all of the values in $data.
  2147 	 *                             A format is one of '%d', '%f', '%s' (integer, float, string).
  2222 	 *                             A format is one of '%d', '%f', '%s' (integer, float, string).
  2148 	 *                             If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  2223 	 *                             If omitted, all values in $data will be treated as strings unless otherwise
  2149 	 * @param string $type         Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
  2224 	 *                             specified in wpdb::$field_types.
       
  2225 	 * @param string       $type   Optional. Type of operation. Possible values include 'INSERT' or 'REPLACE'.
       
  2226 	 *                             Default 'INSERT'.
  2150 	 * @return int|false The number of rows affected, or false on error.
  2227 	 * @return int|false The number of rows affected, or false on error.
  2151 	 */
  2228 	 */
  2152 	function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
  2229 	function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
  2153 		$this->insert_id = 0;
  2230 		$this->insert_id = 0;
  2154 
  2231 
  2155 		if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) {
  2232 		if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) {
  2156 			return false;
  2233 			return false;
  2157 		}
  2234 		}
  2158 
  2235 
  2159 		$data = $this->process_fields( $table, $data, $format );
  2236 		$data = $this->process_fields( $table, $data, $format );
  2160 		if ( false === $data ) {
  2237 		if ( false === $data ) {
  2161 			return false;
  2238 			return false;
  2162 		}
  2239 		}
  2163 
  2240 
  2164 		$formats = $values = array();
  2241 		$formats = array();
       
  2242 		$values  = array();
  2165 		foreach ( $data as $value ) {
  2243 		foreach ( $data as $value ) {
  2166 			if ( is_null( $value['value'] ) ) {
  2244 			if ( is_null( $value['value'] ) ) {
  2167 				$formats[] = 'NULL';
  2245 				$formats[] = 'NULL';
  2168 				continue;
  2246 				continue;
  2169 			}
  2247 			}
  2180 		$this->check_current_query = false;
  2258 		$this->check_current_query = false;
  2181 		return $this->query( $this->prepare( $sql, $values ) );
  2259 		return $this->query( $this->prepare( $sql, $values ) );
  2182 	}
  2260 	}
  2183 
  2261 
  2184 	/**
  2262 	/**
  2185 	 * Update a row in the table
  2263 	 * Updates a row in the table.
  2186 	 *
  2264 	 *
       
  2265 	 * Examples:
  2187 	 *     wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
  2266 	 *     wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
  2188 	 *     wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
  2267 	 *     wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
  2189 	 *
  2268 	 *
  2190 	 * @since 2.5.0
  2269 	 * @since 2.5.0
       
  2270 	 *
  2191 	 * @see wpdb::prepare()
  2271 	 * @see wpdb::prepare()
  2192 	 * @see wpdb::$field_types
  2272 	 * @see wpdb::$field_types
  2193 	 * @see wp_set_wpdb_vars()
  2273 	 * @see wp_set_wpdb_vars()
  2194 	 *
  2274 	 *
  2195 	 * @param string       $table        Table name
  2275 	 * @param string       $table        Table name.
  2196 	 * @param array        $data         Data to update (in column => value pairs).
  2276 	 * @param array        $data         Data to update (in column => value pairs).
  2197 	 *                                   Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2277 	 *                                   Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  2198 	 *                                   Sending a null value will cause the column to be set to NULL - the corresponding
  2278 	 *                                   Sending a null value will cause the column to be set to NULL - the corresponding
  2199 	 *                                   format is ignored in this case.
  2279 	 *                                   format is ignored in this case.
  2200 	 * @param array        $where        A named array of WHERE clauses (in column => value pairs).
  2280 	 * @param array        $where        A named array of WHERE clauses (in column => value pairs).
  2201 	 *                                   Multiple clauses will be joined with ANDs.
  2281 	 *                                   Multiple clauses will be joined with ANDs.
  2202 	 *                                   Both $where columns and $where values should be "raw".
  2282 	 *                                   Both $where columns and $where values should be "raw".
  2203 	 *                                   Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case.
  2283 	 *                                   Sending a null value will create an IS NULL comparison - the corresponding
       
  2284 	 *                                   format will be ignored in this case.
  2204 	 * @param array|string $format       Optional. An array of formats to be mapped to each of the values in $data.
  2285 	 * @param array|string $format       Optional. An array of formats to be mapped to each of the values in $data.
  2205 	 *                                   If string, that format will be used for all of the values in $data.
  2286 	 *                                   If string, that format will be used for all of the values in $data.
  2206 	 *                                   A format is one of '%d', '%f', '%s' (integer, float, string).
  2287 	 *                                   A format is one of '%d', '%f', '%s' (integer, float, string).
  2207 	 *                                   If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
  2288 	 *                                   If omitted, all values in $data will be treated as strings unless otherwise
       
  2289 	 *                                   specified in wpdb::$field_types.
  2208 	 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where.
  2290 	 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where.
  2209 	 *                                   If string, that format will be used for all of the items in $where.
  2291 	 *                                   If string, that format will be used for all of the items in $where.
  2210 	 *                                   A format is one of '%d', '%f', '%s' (integer, float, string).
  2292 	 *                                   A format is one of '%d', '%f', '%s' (integer, float, string).
  2211 	 *                                   If omitted, all values in $where will be treated as strings.
  2293 	 *                                   If omitted, all values in $where will be treated as strings.
  2212 	 * @return int|false The number of rows updated, or false on error.
  2294 	 * @return int|false The number of rows updated, or false on error.
  2223 		$where = $this->process_fields( $table, $where, $where_format );
  2305 		$where = $this->process_fields( $table, $where, $where_format );
  2224 		if ( false === $where ) {
  2306 		if ( false === $where ) {
  2225 			return false;
  2307 			return false;
  2226 		}
  2308 		}
  2227 
  2309 
  2228 		$fields = $conditions = $values = array();
  2310 		$fields     = array();
       
  2311 		$conditions = array();
       
  2312 		$values     = array();
  2229 		foreach ( $data as $field => $value ) {
  2313 		foreach ( $data as $field => $value ) {
  2230 			if ( is_null( $value['value'] ) ) {
  2314 			if ( is_null( $value['value'] ) ) {
  2231 				$fields[] = "`$field` = NULL";
  2315 				$fields[] = "`$field` = NULL";
  2232 				continue;
  2316 				continue;
  2233 			}
  2317 			}
  2253 		$this->check_current_query = false;
  2337 		$this->check_current_query = false;
  2254 		return $this->query( $this->prepare( $sql, $values ) );
  2338 		return $this->query( $this->prepare( $sql, $values ) );
  2255 	}
  2339 	}
  2256 
  2340 
  2257 	/**
  2341 	/**
  2258 	 * Delete a row in the table
  2342 	 * Deletes a row in the table.
  2259 	 *
  2343 	 *
       
  2344 	 * Examples:
  2260 	 *     wpdb::delete( 'table', array( 'ID' => 1 ) )
  2345 	 *     wpdb::delete( 'table', array( 'ID' => 1 ) )
  2261 	 *     wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
  2346 	 *     wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
  2262 	 *
  2347 	 *
  2263 	 * @since 3.4.0
  2348 	 * @since 3.4.0
       
  2349 	 *
  2264 	 * @see wpdb::prepare()
  2350 	 * @see wpdb::prepare()
  2265 	 * @see wpdb::$field_types
  2351 	 * @see wpdb::$field_types
  2266 	 * @see wp_set_wpdb_vars()
  2352 	 * @see wp_set_wpdb_vars()
  2267 	 *
  2353 	 *
  2268 	 * @param string       $table        Table name
  2354 	 * @param string       $table        Table name.
  2269 	 * @param array        $where        A named array of WHERE clauses (in column => value pairs).
  2355 	 * @param array        $where        A named array of WHERE clauses (in column => value pairs).
  2270 	 *                                   Multiple clauses will be joined with ANDs.
  2356 	 *                                   Multiple clauses will be joined with ANDs.
  2271 	 *                                   Both $where columns and $where values should be "raw".
  2357 	 *                                   Both $where columns and $where values should be "raw".
  2272 	 *                                   Sending a null value will create an IS NULL comparison - the corresponding format will be ignored in this case.
  2358 	 *                                   Sending a null value will create an IS NULL comparison - the corresponding
       
  2359 	 *                                   format will be ignored in this case.
  2273 	 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where.
  2360 	 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where.
  2274 	 *                                   If string, that format will be used for all of the items in $where.
  2361 	 *                                   If string, that format will be used for all of the items in $where.
  2275 	 *                                   A format is one of '%d', '%f', '%s' (integer, float, string).
  2362 	 *                                   A format is one of '%d', '%f', '%s' (integer, float, string).
  2276 	 *                                   If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.
  2363 	 *                                   If omitted, all values in $data will be treated as strings unless otherwise
       
  2364 	 *                                   specified in wpdb::$field_types.
  2277 	 * @return int|false The number of rows updated, or false on error.
  2365 	 * @return int|false The number of rows updated, or false on error.
  2278 	 */
  2366 	 */
  2279 	public function delete( $table, $where, $where_format = null ) {
  2367 	public function delete( $table, $where, $where_format = null ) {
  2280 		if ( ! is_array( $where ) ) {
  2368 		if ( ! is_array( $where ) ) {
  2281 			return false;
  2369 			return false;
  2284 		$where = $this->process_fields( $table, $where, $where_format );
  2372 		$where = $this->process_fields( $table, $where, $where_format );
  2285 		if ( false === $where ) {
  2373 		if ( false === $where ) {
  2286 			return false;
  2374 			return false;
  2287 		}
  2375 		}
  2288 
  2376 
  2289 		$conditions = $values = array();
  2377 		$conditions = array();
       
  2378 		$values     = array();
  2290 		foreach ( $where as $field => $value ) {
  2379 		foreach ( $where as $field => $value ) {
  2291 			if ( is_null( $value['value'] ) ) {
  2380 			if ( is_null( $value['value'] ) ) {
  2292 				$conditions[] = "`$field` IS NULL";
  2381 				$conditions[] = "`$field` IS NULL";
  2293 				continue;
  2382 				continue;
  2294 			}
  2383 			}
  2306 	}
  2395 	}
  2307 
  2396 
  2308 	/**
  2397 	/**
  2309 	 * Processes arrays of field/value pairs and field formats.
  2398 	 * Processes arrays of field/value pairs and field formats.
  2310 	 *
  2399 	 *
  2311 	 * This is a helper method for wpdb's CRUD methods, which take field/value
  2400 	 * This is a helper method for wpdb's CRUD methods, which take field/value pairs
  2312 	 * pairs for inserts, updates, and where clauses. This method first pairs
  2401 	 * for inserts, updates, and where clauses. This method first pairs each value
  2313 	 * each value with a format. Then it determines the charset of that field,
  2402 	 * with a format. Then it determines the charset of that field, using that
  2314 	 * using that to determine if any invalid text would be stripped. If text is
  2403 	 * to determine if any invalid text would be stripped. If text is stripped,
  2315 	 * stripped, then field processing is rejected and the query fails.
  2404 	 * then field processing is rejected and the query fails.
  2316 	 *
  2405 	 *
  2317 	 * @since 4.2.0
  2406 	 * @since 4.2.0
  2318 	 *
  2407 	 *
  2319 	 * @param string $table  Table name.
  2408 	 * @param string $table  Table name.
  2320 	 * @param array  $data   Field/value pair.
  2409 	 * @param array  $data   Field/value pair.
  2321 	 * @param mixed  $format Format for each field.
  2410 	 * @param mixed  $format Format for each field.
  2322 	 * @return array|false Returns an array of fields that contain paired values
  2411 	 * @return array|false An array of fields that contain paired value and formats.
  2323 	 *                    and formats. Returns false for invalid values.
  2412 	 *                     False for invalid values.
  2324 	 */
  2413 	 */
  2325 	protected function process_fields( $table, $data, $format ) {
  2414 	protected function process_fields( $table, $data, $format ) {
  2326 		$data = $this->process_field_formats( $data, $format );
  2415 		$data = $this->process_field_formats( $data, $format );
  2327 		if ( false === $data ) {
  2416 		if ( false === $data ) {
  2328 			return false;
  2417 			return false;
  2356 	 * @param mixed $format Formats to be mapped to the values in $data.
  2445 	 * @param mixed $format Formats to be mapped to the values in $data.
  2357 	 * @return array Array, keyed by field names with values being an array
  2446 	 * @return array Array, keyed by field names with values being an array
  2358 	 *               of 'value' and 'format' keys.
  2447 	 *               of 'value' and 'format' keys.
  2359 	 */
  2448 	 */
  2360 	protected function process_field_formats( $data, $format ) {
  2449 	protected function process_field_formats( $data, $format ) {
  2361 		$formats = $original_formats = (array) $format;
  2450 		$formats          = (array) $format;
       
  2451 		$original_formats = $formats;
  2362 
  2452 
  2363 		foreach ( $data as $field => $value ) {
  2453 		foreach ( $data as $field => $value ) {
  2364 			$value = array(
  2454 			$value = array(
  2365 				'value'  => $value,
  2455 				'value'  => $value,
  2366 				'format' => '%s',
  2456 				'format' => '%s',
  2380 
  2470 
  2381 		return $data;
  2471 		return $data;
  2382 	}
  2472 	}
  2383 
  2473 
  2384 	/**
  2474 	/**
  2385 	 * Adds field charsets to field/value/format arrays generated by
  2475 	 * Adds field charsets to field/value/format arrays generated by wpdb::process_field_formats().
  2386 	 * the wpdb::process_field_formats() method.
       
  2387 	 *
  2476 	 *
  2388 	 * @since 4.2.0
  2477 	 * @since 4.2.0
  2389 	 *
  2478 	 *
  2390 	 * @param array  $data  As it comes from the wpdb::process_field_formats() method.
  2479 	 * @param array  $data  As it comes from the wpdb::process_field_formats() method.
  2391 	 * @param string $table Table name.
  2480 	 * @param string $table Table name.
  2392 	 * @return array|false The same array as $data with additional 'charset' keys.
  2481 	 * @return array|false The same array as $data with additional 'charset' keys.
       
  2482 	 *                     False on failure.
  2393 	 */
  2483 	 */
  2394 	protected function process_field_charsets( $data, $table ) {
  2484 	protected function process_field_charsets( $data, $table ) {
  2395 		foreach ( $data as $field => $value ) {
  2485 		foreach ( $data as $field => $value ) {
  2396 			if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
  2486 			if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
  2397 				/*
  2487 				/*
  2411 
  2501 
  2412 		return $data;
  2502 		return $data;
  2413 	}
  2503 	}
  2414 
  2504 
  2415 	/**
  2505 	/**
  2416 	 * For string fields, record the maximum string length that field can safely save.
  2506 	 * For string fields, records the maximum string length that field can safely save.
  2417 	 *
  2507 	 *
  2418 	 * @since 4.2.1
  2508 	 * @since 4.2.1
  2419 	 *
  2509 	 *
  2420 	 * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
  2510 	 * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
  2421 	 * @param string $table Table name.
  2511 	 * @param string $table Table name.
  2442 
  2532 
  2443 		return $data;
  2533 		return $data;
  2444 	}
  2534 	}
  2445 
  2535 
  2446 	/**
  2536 	/**
  2447 	 * Retrieve one variable from the database.
  2537 	 * Retrieves one variable from the database.
  2448 	 *
  2538 	 *
  2449 	 * Executes a SQL query and returns the value from the SQL result.
  2539 	 * Executes a SQL query and returns the value from the SQL result.
  2450 	 * 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.
  2540 	 * If the SQL result contains more than one column and/or more than one row,
  2451 	 * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
  2541 	 * the value in the column and row specified is returned. If $query is null,
       
  2542 	 * the value in the specified column and row from the previous SQL result is returned.
  2452 	 *
  2543 	 *
  2453 	 * @since 0.71
  2544 	 * @since 0.71
  2454 	 *
  2545 	 *
  2455 	 * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
  2546 	 * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
  2456 	 * @param int         $x     Optional. Column of value to return. Indexed from 0.
  2547 	 * @param int         $x     Optional. Column of value to return. Indexed from 0.
  2457 	 * @param int         $y     Optional. Row of value to return. Indexed from 0.
  2548 	 * @param int         $y     Optional. Row of value to return. Indexed from 0.
  2458 	 * @return string|null Database query result (as string), or null on failure
  2549 	 * @return string|null Database query result (as string), or null on failure.
  2459 	 */
  2550 	 */
  2460 	public function get_var( $query = null, $x = 0, $y = 0 ) {
  2551 	public function get_var( $query = null, $x = 0, $y = 0 ) {
  2461 		$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
  2552 		$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
  2462 
  2553 
  2463 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
  2554 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
  2466 
  2557 
  2467 		if ( $query ) {
  2558 		if ( $query ) {
  2468 			$this->query( $query );
  2559 			$this->query( $query );
  2469 		}
  2560 		}
  2470 
  2561 
  2471 		// Extract var out of cached results based x,y vals
  2562 		// Extract var out of cached results based on x,y vals.
  2472 		if ( ! empty( $this->last_result[ $y ] ) ) {
  2563 		if ( ! empty( $this->last_result[ $y ] ) ) {
  2473 			$values = array_values( get_object_vars( $this->last_result[ $y ] ) );
  2564 			$values = array_values( get_object_vars( $this->last_result[ $y ] ) );
  2474 		}
  2565 		}
  2475 
  2566 
  2476 		// If there is a value return it else return null
  2567 		// If there is a value return it, else return null.
  2477 		return ( isset( $values[ $x ] ) && $values[ $x ] !== '' ) ? $values[ $x ] : null;
  2568 		return ( isset( $values[ $x ] ) && '' !== $values[ $x ] ) ? $values[ $x ] : null;
  2478 	}
  2569 	}
  2479 
  2570 
  2480 	/**
  2571 	/**
  2481 	 * Retrieve one row from the database.
  2572 	 * Retrieves one row from the database.
  2482 	 *
  2573 	 *
  2483 	 * Executes a SQL query and returns the row from the SQL result.
  2574 	 * Executes a SQL query and returns the row from the SQL result.
  2484 	 *
  2575 	 *
  2485 	 * @since 0.71
  2576 	 * @since 0.71
  2486 	 *
  2577 	 *
  2487 	 * @param string|null $query  SQL query.
  2578 	 * @param string|null $query  SQL query.
  2488 	 * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
  2579 	 * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  2489 	 *                            an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT.
  2580 	 *                            correspond to an stdClass object, an associative array, or a numeric array,
       
  2581 	 *                            respectively. Default OBJECT.
  2490 	 * @param int         $y      Optional. Row to return. Indexed from 0.
  2582 	 * @param int         $y      Optional. Row to return. Indexed from 0.
  2491 	 * @return array|object|null|void Database query result in format specified by $output or null on failure
  2583 	 * @return array|object|null|void Database query result in format specified by $output or null on failure.
  2492 	 */
  2584 	 */
  2493 	public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
  2585 	public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
  2494 		$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  2586 		$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  2495 
  2587 
  2496 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
  2588 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
  2505 
  2597 
  2506 		if ( ! isset( $this->last_result[ $y ] ) ) {
  2598 		if ( ! isset( $this->last_result[ $y ] ) ) {
  2507 			return null;
  2599 			return null;
  2508 		}
  2600 		}
  2509 
  2601 
  2510 		if ( $output == OBJECT ) {
  2602 		if ( OBJECT === $output ) {
  2511 			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
  2603 			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
  2512 		} elseif ( $output == ARRAY_A ) {
  2604 		} elseif ( ARRAY_A === $output ) {
  2513 			return $this->last_result[ $y ] ? get_object_vars( $this->last_result[ $y ] ) : null;
  2605 			return $this->last_result[ $y ] ? get_object_vars( $this->last_result[ $y ] ) : null;
  2514 		} elseif ( $output == ARRAY_N ) {
  2606 		} elseif ( ARRAY_N === $output ) {
  2515 			return $this->last_result[ $y ] ? array_values( get_object_vars( $this->last_result[ $y ] ) ) : null;
  2607 			return $this->last_result[ $y ] ? array_values( get_object_vars( $this->last_result[ $y ] ) ) : null;
  2516 		} elseif ( strtoupper( $output ) === OBJECT ) {
  2608 		} elseif ( OBJECT === strtoupper( $output ) ) {
  2517 			// Back compat for OBJECT being previously case insensitive.
  2609 			// Back compat for OBJECT being previously case-insensitive.
  2518 			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
  2610 			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
  2519 		} else {
  2611 		} else {
  2520 			$this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' );
  2612 			$this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' );
  2521 		}
  2613 		}
  2522 	}
  2614 	}
  2523 
  2615 
  2524 	/**
  2616 	/**
  2525 	 * Retrieve one column from the database.
  2617 	 * Retrieves one column from the database.
  2526 	 *
  2618 	 *
  2527 	 * Executes a SQL query and returns the column from the SQL result.
  2619 	 * Executes a SQL query and returns the column from the SQL result.
  2528 	 * If the SQL result contains more than one column, this function returns the column specified.
  2620 	 * If the SQL result contains more than one column, the column specified is returned.
  2529 	 * If $query is null, this function returns the specified column from the previous SQL result.
  2621 	 * If $query is null, the specified column from the previous SQL result is returned.
  2530 	 *
  2622 	 *
  2531 	 * @since 0.71
  2623 	 * @since 0.71
  2532 	 *
  2624 	 *
  2533 	 * @param string|null $query Optional. SQL query. Defaults to previous query.
  2625 	 * @param string|null $query Optional. SQL query. Defaults to previous query.
  2534 	 * @param int         $x     Optional. Column to return. Indexed from 0.
  2626 	 * @param int         $x     Optional. Column to return. Indexed from 0.
  2542 		if ( $query ) {
  2634 		if ( $query ) {
  2543 			$this->query( $query );
  2635 			$this->query( $query );
  2544 		}
  2636 		}
  2545 
  2637 
  2546 		$new_array = array();
  2638 		$new_array = array();
  2547 		// Extract the column values
  2639 		// Extract the column values.
  2548 		if ( $this->last_result ) {
  2640 		if ( $this->last_result ) {
  2549 			for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
  2641 			for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
  2550 				$new_array[ $i ] = $this->get_var( null, $x, $i );
  2642 				$new_array[ $i ] = $this->get_var( null, $x, $i );
  2551 			}
  2643 			}
  2552 		}
  2644 		}
  2553 		return $new_array;
  2645 		return $new_array;
  2554 	}
  2646 	}
  2555 
  2647 
  2556 	/**
  2648 	/**
  2557 	 * Retrieve an entire SQL result set from the database (i.e., many rows)
  2649 	 * Retrieves an entire SQL result set from the database (i.e., many rows).
  2558 	 *
  2650 	 *
  2559 	 * Executes a SQL query and returns the entire SQL result.
  2651 	 * Executes a SQL query and returns the entire SQL result.
  2560 	 *
  2652 	 *
  2561 	 * @since 0.71
  2653 	 * @since 0.71
  2562 	 *
  2654 	 *
  2563 	 * @param string $query  SQL query.
  2655 	 * @param string $query  SQL query.
  2564 	 * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.
  2656 	 * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.
  2565 	 *                       With one of the first three, return an array of rows indexed from 0 by SQL result row number.
  2657 	 *                       With one of the first three, return an array of rows indexed
  2566 	 *                       Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively.
  2658 	 *                       from 0 by SQL result row number. Each row is an associative array
  2567 	 *                       With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value.
  2659 	 *                       (column => value, ...), a numerically indexed array (0 => value, ...),
  2568 	 *                       Duplicate keys are discarded.
  2660 	 *                       or an object ( ->column = value ), respectively. With OBJECT_K,
  2569 	 * @return array|object|null Database query results
  2661 	 *                       return an associative array of row objects keyed by the value
       
  2662 	 *                       of each row's first column's value. Duplicate keys are discarded.
       
  2663 	 * @return array|object|null Database query results.
  2570 	 */
  2664 	 */
  2571 	public function get_results( $query = null, $output = OBJECT ) {
  2665 	public function get_results( $query = null, $output = OBJECT ) {
  2572 		$this->func_call = "\$db->get_results(\"$query\", $output)";
  2666 		$this->func_call = "\$db->get_results(\"$query\", $output)";
  2573 
  2667 
  2574 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
  2668 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
  2580 		} else {
  2674 		} else {
  2581 			return null;
  2675 			return null;
  2582 		}
  2676 		}
  2583 
  2677 
  2584 		$new_array = array();
  2678 		$new_array = array();
  2585 		if ( $output == OBJECT ) {
  2679 		if ( OBJECT === $output ) {
  2586 			// Return an integer-keyed array of row objects
  2680 			// Return an integer-keyed array of row objects.
  2587 			return $this->last_result;
  2681 			return $this->last_result;
  2588 		} elseif ( $output == OBJECT_K ) {
  2682 		} elseif ( OBJECT_K === $output ) {
  2589 			// Return an array of row objects with keys from column 1
  2683 			// Return an array of row objects with keys from column 1.
  2590 			// (Duplicates are discarded)
  2684 			// (Duplicates are discarded.)
  2591 			if ( $this->last_result ) {
  2685 			if ( $this->last_result ) {
  2592 				foreach ( $this->last_result as $row ) {
  2686 				foreach ( $this->last_result as $row ) {
  2593 					$var_by_ref = get_object_vars( $row );
  2687 					$var_by_ref = get_object_vars( $row );
  2594 					$key        = array_shift( $var_by_ref );
  2688 					$key        = array_shift( $var_by_ref );
  2595 					if ( ! isset( $new_array[ $key ] ) ) {
  2689 					if ( ! isset( $new_array[ $key ] ) ) {
  2596 						$new_array[ $key ] = $row;
  2690 						$new_array[ $key ] = $row;
  2597 					}
  2691 					}
  2598 				}
  2692 				}
  2599 			}
  2693 			}
  2600 			return $new_array;
  2694 			return $new_array;
  2601 		} elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  2695 		} elseif ( ARRAY_A === $output || ARRAY_N === $output ) {
  2602 			// Return an integer-keyed array of...
  2696 			// Return an integer-keyed array of...
  2603 			if ( $this->last_result ) {
  2697 			if ( $this->last_result ) {
  2604 				foreach ( (array) $this->last_result as $row ) {
  2698 				foreach ( (array) $this->last_result as $row ) {
  2605 					if ( $output == ARRAY_N ) {
  2699 					if ( ARRAY_N === $output ) {
  2606 						// ...integer-keyed row arrays
  2700 						// ...integer-keyed row arrays.
  2607 						$new_array[] = array_values( get_object_vars( $row ) );
  2701 						$new_array[] = array_values( get_object_vars( $row ) );
  2608 					} else {
  2702 					} else {
  2609 						// ...column name-keyed row arrays
  2703 						// ...column name-keyed row arrays.
  2610 						$new_array[] = get_object_vars( $row );
  2704 						$new_array[] = get_object_vars( $row );
  2611 					}
  2705 					}
  2612 				}
  2706 				}
  2613 			}
  2707 			}
  2614 			return $new_array;
  2708 			return $new_array;
  2615 		} elseif ( strtoupper( $output ) === OBJECT ) {
  2709 		} elseif ( strtoupper( $output ) === OBJECT ) {
  2616 			// Back compat for OBJECT being previously case insensitive.
  2710 			// Back compat for OBJECT being previously case-insensitive.
  2617 			return $this->last_result;
  2711 			return $this->last_result;
  2618 		}
  2712 		}
  2619 		return null;
  2713 		return null;
  2620 	}
  2714 	}
  2621 
  2715 
  2636 		 * Passing a non-null value to the filter will effectively short-circuit
  2730 		 * Passing a non-null value to the filter will effectively short-circuit
  2637 		 * checking the DB for the charset, returning that value instead.
  2731 		 * checking the DB for the charset, returning that value instead.
  2638 		 *
  2732 		 *
  2639 		 * @since 4.2.0
  2733 		 * @since 4.2.0
  2640 		 *
  2734 		 *
  2641 		 * @param string $charset The character set to use. Default null.
  2735 		 * @param string|null $charset The character set to use. Default null.
  2642 		 * @param string $table   The name of the table being checked.
  2736 		 * @param string      $table   The name of the table being checked.
  2643 		 */
  2737 		 */
  2644 		$charset = apply_filters( 'pre_get_table_charset', null, $table );
  2738 		$charset = apply_filters( 'pre_get_table_charset', null, $table );
  2645 		if ( null !== $charset ) {
  2739 		if ( null !== $charset ) {
  2646 			return $charset;
  2740 			return $charset;
  2647 		}
  2741 		}
  2648 
  2742 
  2649 		if ( isset( $this->table_charset[ $tablekey ] ) ) {
  2743 		if ( isset( $this->table_charset[ $tablekey ] ) ) {
  2650 			return $this->table_charset[ $tablekey ];
  2744 			return $this->table_charset[ $tablekey ];
  2651 		}
  2745 		}
  2652 
  2746 
  2653 		$charsets = $columns = array();
  2747 		$charsets = array();
       
  2748 		$columns  = array();
  2654 
  2749 
  2655 		$table_parts = explode( '.', $table );
  2750 		$table_parts = explode( '.', $table );
  2656 		$table       = '`' . implode( '`.`', $table_parts ) . '`';
  2751 		$table       = '`' . implode( '`.`', $table_parts ) . '`';
  2657 		$results     = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
  2752 		$results     = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
  2658 		if ( ! $results ) {
  2753 		if ( ! $results ) {
  2678 			}
  2773 			}
  2679 
  2774 
  2680 			list( $type ) = explode( '(', $column->Type );
  2775 			list( $type ) = explode( '(', $column->Type );
  2681 
  2776 
  2682 			// A binary/blob means the whole query gets treated like this.
  2777 			// A binary/blob means the whole query gets treated like this.
  2683 			if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ) ) ) {
  2778 			if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ), true ) ) {
  2684 				$this->table_charset[ $tablekey ] = 'binary';
  2779 				$this->table_charset[ $tablekey ] = 'binary';
  2685 				return 'binary';
  2780 				return 'binary';
  2686 			}
  2781 			}
  2687 		}
  2782 		}
  2688 
  2783 
  2724 	 *
  2819 	 *
  2725 	 * @since 4.2.0
  2820 	 * @since 4.2.0
  2726 	 *
  2821 	 *
  2727 	 * @param string $table  Table name.
  2822 	 * @param string $table  Table name.
  2728 	 * @param string $column Column name.
  2823 	 * @param string $column Column name.
  2729 	 * @return string|false|WP_Error Column character set as a string. False if the column has no
  2824 	 * @return string|false|WP_Error Column character set as a string. False if the column has
  2730 	 *                               character set. WP_Error object if there was an error.
  2825 	 *                               no character set. WP_Error object if there was an error.
  2731 	 */
  2826 	 */
  2732 	public function get_col_charset( $table, $column ) {
  2827 	public function get_col_charset( $table, $column ) {
  2733 		$tablekey  = strtolower( $table );
  2828 		$tablekey  = strtolower( $table );
  2734 		$columnkey = strtolower( $column );
  2829 		$columnkey = strtolower( $column );
  2735 
  2830 
  2739 		 * Passing a non-null value to the filter will short-circuit
  2834 		 * Passing a non-null value to the filter will short-circuit
  2740 		 * checking the DB for the charset, returning that value instead.
  2835 		 * checking the DB for the charset, returning that value instead.
  2741 		 *
  2836 		 *
  2742 		 * @since 4.2.0
  2837 		 * @since 4.2.0
  2743 		 *
  2838 		 *
  2744 		 * @param string $charset The character set to use. Default null.
  2839 		 * @param string|null $charset The character set to use. Default null.
  2745 		 * @param string $table   The name of the table being checked.
  2840 		 * @param string      $table   The name of the table being checked.
  2746 		 * @param string $column  The name of the column being checked.
  2841 		 * @param string      $column  The name of the column being checked.
  2747 		 */
  2842 		 */
  2748 		$charset = apply_filters( 'pre_get_col_charset', null, $table, $column );
  2843 		$charset = apply_filters( 'pre_get_col_charset', null, $table, $column );
  2749 		if ( null !== $charset ) {
  2844 		if ( null !== $charset ) {
  2750 			return $charset;
  2845 			return $charset;
  2751 		}
  2846 		}
  2781 		list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation );
  2876 		list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation );
  2782 		return $charset;
  2877 		return $charset;
  2783 	}
  2878 	}
  2784 
  2879 
  2785 	/**
  2880 	/**
  2786 	 * Retrieve the maximum string length allowed in a given column.
  2881 	 * Retrieves the maximum string length allowed in a given column.
       
  2882 	 *
  2787 	 * The length may either be specified as a byte length or a character length.
  2883 	 * The length may either be specified as a byte length or a character length.
  2788 	 *
  2884 	 *
  2789 	 * @since 4.2.1
  2885 	 * @since 4.2.1
  2790 	 *
  2886 	 *
  2791 	 * @param string $table  Table name.
  2887 	 * @param string $table  Table name.
  2792 	 * @param string $column Column name.
  2888 	 * @param string $column Column name.
  2793 	 * @return array|false|WP_Error array( 'length' => (int), 'type' => 'byte' | 'char' )
  2889 	 * @return array|false|WP_Error array( 'length' => (int), 'type' => 'byte' | 'char' ).
  2794 	 *                              false if the column has no length (for example, numeric column)
  2890 	 *                              False if the column has no length (for example, numeric column).
  2795 	 *                              WP_Error object if there was an error.
  2891 	 *                              WP_Error object if there was an error.
  2796 	 */
  2892 	 */
  2797 	public function get_col_length( $table, $column ) {
  2893 	public function get_col_length( $table, $column ) {
  2798 		$tablekey  = strtolower( $table );
  2894 		$tablekey  = strtolower( $table );
  2799 		$columnkey = strtolower( $column );
  2895 		$columnkey = strtolower( $column );
  2871 				return false;
  2967 				return false;
  2872 		}
  2968 		}
  2873 	}
  2969 	}
  2874 
  2970 
  2875 	/**
  2971 	/**
  2876 	 * Check if a string is ASCII.
  2972 	 * Checks if a string is ASCII.
  2877 	 *
  2973 	 *
  2878 	 * The negative regex is faster for non-ASCII strings, as it allows
  2974 	 * The negative regex is faster for non-ASCII strings, as it allows
  2879 	 * the search to finish as soon as it encounters a non-ASCII character.
  2975 	 * the search to finish as soon as it encounters a non-ASCII character.
  2880 	 *
  2976 	 *
  2881 	 * @since 4.2.0
  2977 	 * @since 4.2.0
  2894 
  2990 
  2895 		return false;
  2991 		return false;
  2896 	}
  2992 	}
  2897 
  2993 
  2898 	/**
  2994 	/**
  2899 	 * Check if the query is accessing a collation considered safe on the current version of MySQL.
  2995 	 * Checks if the query is accessing a collation considered safe on the current version of MySQL.
  2900 	 *
  2996 	 *
  2901 	 * @since 4.2.0
  2997 	 * @since 4.2.0
  2902 	 *
  2998 	 *
  2903 	 * @param string $query The query to check.
  2999 	 * @param string $query The query to check.
  2904 	 * @return bool True if the collation is safe, false if it isn't.
  3000 	 * @return bool True if the collation is safe, false if it isn't.
  2955 	/**
  3051 	/**
  2956 	 * Strips any invalid characters based on value/charset pairs.
  3052 	 * Strips any invalid characters based on value/charset pairs.
  2957 	 *
  3053 	 *
  2958 	 * @since 4.2.0
  3054 	 * @since 4.2.0
  2959 	 *
  3055 	 *
  2960 	 * @param array $data Array of value arrays. Each value array has the keys
  3056 	 * @param array $data Array of value arrays. Each value array has the keys 'value' and 'charset'.
  2961 	 *                    'value' and 'charset'. An optional 'ascii' key can be
  3057 	 *                    An optional 'ascii' key can be set to false to avoid redundant ASCII checks.
  2962 	 *                    set to false to avoid redundant ASCII checks.
  3058 	 * @return array|WP_Error The $data parameter, with invalid characters removed from each value.
  2963 	 * @return array|WP_Error The $data parameter, with invalid characters removed from
  3059 	 *                        This works as a passthrough: any additional keys such as 'field' are
  2964 	 *                        each value. This works as a passthrough: any additional keys
  3060 	 *                        retained in each value array. If we cannot remove invalid characters,
  2965 	 *                        such as 'field' are retained in each value array. If we cannot
  3061 	 *                        a WP_Error object is returned.
  2966 	 *                        remove invalid characters, a WP_Error object is returned.
       
  2967 	 */
  3062 	 */
  2968 	protected function strip_invalid_text( $data ) {
  3063 	protected function strip_invalid_text( $data ) {
  2969 		$db_check_string = false;
  3064 		$db_check_string = false;
  2970 
  3065 
  2971 		foreach ( $data as &$value ) {
  3066 		foreach ( $data as &$value ) {
  2974 			if ( is_array( $value['length'] ) ) {
  3069 			if ( is_array( $value['length'] ) ) {
  2975 				$length                  = $value['length']['length'];
  3070 				$length                  = $value['length']['length'];
  2976 				$truncate_by_byte_length = 'byte' === $value['length']['type'];
  3071 				$truncate_by_byte_length = 'byte' === $value['length']['type'];
  2977 			} else {
  3072 			} else {
  2978 				$length = false;
  3073 				$length = false;
  2979 				// Since we have no length, we'll never truncate.
  3074 				// Since we have no length, we'll never truncate. Initialize the variable to false.
  2980 				// Initialize the variable to false. true would take us
  3075 				// True would take us through an unnecessary (for this case) codepath below.
  2981 				// through an unnecessary (for this case) codepath below.
       
  2982 				$truncate_by_byte_length = false;
  3076 				$truncate_by_byte_length = false;
  2983 			}
  3077 			}
  2984 
  3078 
  2985 			// There's no charset to work with.
  3079 			// There's no charset to work with.
  2986 			if ( false === $charset ) {
  3080 			if ( false === $charset ) {
  2992 				continue;
  3086 				continue;
  2993 			}
  3087 			}
  2994 
  3088 
  2995 			$needs_validation = true;
  3089 			$needs_validation = true;
  2996 			if (
  3090 			if (
  2997 				// latin1 can store any byte sequence
  3091 				// latin1 can store any byte sequence.
  2998 				'latin1' === $charset
  3092 				'latin1' === $charset
  2999 			||
  3093 			||
  3000 				// ASCII is always OK.
  3094 				// ASCII is always OK.
  3001 				( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) )
  3095 				( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) )
  3002 			) {
  3096 			) {
  3046 				}
  3140 				}
  3047 				continue;
  3141 				continue;
  3048 			}
  3142 			}
  3049 
  3143 
  3050 			// We couldn't use any local conversions, send it to the DB.
  3144 			// We couldn't use any local conversions, send it to the DB.
  3051 			$value['db'] = $db_check_string = true;
  3145 			$value['db']     = true;
       
  3146 			$db_check_string = true;
  3052 		}
  3147 		}
  3053 		unset( $value ); // Remove by reference.
  3148 		unset( $value ); // Remove by reference.
  3054 
  3149 
  3055 		if ( $db_check_string ) {
  3150 		if ( $db_check_string ) {
  3056 			$queries = array();
  3151 			$queries = array();
  3057 			foreach ( $data as $col => $value ) {
  3152 			foreach ( $data as $col => $value ) {
  3058 				if ( ! empty( $value['db'] ) ) {
  3153 				if ( ! empty( $value['db'] ) ) {
  3059 					// We're going to need to truncate by characters or bytes, depending on the length value we have.
  3154 					// We're going to need to truncate by characters or bytes, depending on the length value we have.
  3060 					if ( 'byte' === $value['length']['type'] ) {
  3155 					if ( isset( $value['length']['type'] ) && 'byte' === $value['length']['type'] ) {
  3061 						// Using binary causes LEFT() to truncate by bytes.
  3156 						// Using binary causes LEFT() to truncate by bytes.
  3062 						$charset = 'binary';
  3157 						$charset = 'binary';
  3063 					} else {
  3158 					} else {
  3064 						$charset = $value['charset'];
  3159 						$charset = $value['charset'];
  3065 					}
  3160 					}
  3131 			$charset = $this->get_table_charset( $table );
  3226 			$charset = $this->get_table_charset( $table );
  3132 			if ( is_wp_error( $charset ) ) {
  3227 			if ( is_wp_error( $charset ) ) {
  3133 				return $charset;
  3228 				return $charset;
  3134 			}
  3229 			}
  3135 
  3230 
  3136 			// We can't reliably strip text from tables containing binary/blob columns
  3231 			// We can't reliably strip text from tables containing binary/blob columns.
  3137 			if ( 'binary' === $charset ) {
  3232 			if ( 'binary' === $charset ) {
  3138 				return $query;
  3233 				return $query;
  3139 			}
  3234 			}
  3140 		} else {
  3235 		} else {
  3141 			$charset = $this->charset;
  3236 			$charset = $this->charset;
  3195 
  3290 
  3196 		return $data[ $column ]['value'];
  3291 		return $data[ $column ]['value'];
  3197 	}
  3292 	}
  3198 
  3293 
  3199 	/**
  3294 	/**
  3200 	 * Find the first table name referenced in a query.
  3295 	 * Finds the first table name referenced in a query.
  3201 	 *
  3296 	 *
  3202 	 * @since 4.2.0
  3297 	 * @since 4.2.0
  3203 	 *
  3298 	 *
  3204 	 * @param string $query The query to search.
  3299 	 * @param string $query The query to search.
  3205 	 * @return string|false $table The table name found, or false if a table couldn't be found.
  3300 	 * @return string|false The table name found, or false if a table couldn't be found.
  3206 	 */
  3301 	 */
  3207 	protected function get_table_from_query( $query ) {
  3302 	protected function get_table_from_query( $query ) {
  3208 		// Remove characters that can legally trail the table name.
  3303 		// Remove characters that can legally trail the table name.
  3209 		$query = rtrim( $query, ';/-#' );
  3304 		$query = rtrim( $query, ';/-#' );
  3210 
  3305 
  3232 		// SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts'
  3327 		// SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts'
  3233 		if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) {
  3328 		if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) {
  3234 			return $maybe[2];
  3329 			return $maybe[2];
  3235 		}
  3330 		}
  3236 
  3331 
  3237 		// SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%'
  3332 		/*
  3238 		// This quoted LIKE operand seldom holds a full table name.
  3333 		 * SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%'
  3239 		// It is usually a pattern for matching a prefix so we just
  3334 		 * This quoted LIKE operand seldom holds a full table name.
  3240 		// strip the trailing % and unescape the _ to get 'wp_123_'
  3335 		 * It is usually a pattern for matching a prefix so we just
  3241 		// which drop-ins can use for routing these SQL statements.
  3336 		 * strip the trailing % and unescape the _ to get 'wp_123_'
       
  3337 		 * which drop-ins can use for routing these SQL statements.
       
  3338 		 */
  3242 		if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) {
  3339 		if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) {
  3243 			return str_replace( '\\_', '_', $maybe[2] );
  3340 			return str_replace( '\\_', '_', $maybe[2] );
  3244 		}
  3341 		}
  3245 
  3342 
  3246 		// Big pattern for the rest of the table-related queries.
  3343 		// Big pattern for the rest of the table-related queries.
  3268 
  3365 
  3269 		return false;
  3366 		return false;
  3270 	}
  3367 	}
  3271 
  3368 
  3272 	/**
  3369 	/**
  3273 	 * Load the column metadata from the last query.
  3370 	 * Loads the column metadata from the last query.
  3274 	 *
  3371 	 *
  3275 	 * @since 3.5.0
  3372 	 * @since 3.5.0
  3276 	 */
  3373 	 */
  3277 	protected function load_col_info() {
  3374 	protected function load_col_info() {
  3278 		if ( $this->col_info ) {
  3375 		if ( $this->col_info ) {
  3291 			}
  3388 			}
  3292 		}
  3389 		}
  3293 	}
  3390 	}
  3294 
  3391 
  3295 	/**
  3392 	/**
  3296 	 * Retrieve column metadata from the last query.
  3393 	 * Retrieves column metadata from the last query.
  3297 	 *
  3394 	 *
  3298 	 * @since 0.71
  3395 	 * @since 0.71
  3299 	 *
  3396 	 *
  3300 	 * @param string $info_type  Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
  3397 	 * @param string $info_type  Optional. Possible values include 'name', 'table', 'def', 'max_length',
  3301 	 * @param int    $col_offset Optional. 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
  3398 	 *                           'not_null', 'primary_key', 'multiple_key', 'unique_key', 'numeric',
  3302 	 * @return mixed Column Results
  3399 	 *                           'blob', 'type', 'unsigned', 'zerofill'. Default 'name'.
       
  3400 	 * @param int    $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length.
       
  3401 	 *                           3: if the col is numeric. 4: col's type. Default -1.
       
  3402 	 * @return mixed Column results.
  3303 	 */
  3403 	 */
  3304 	public function get_col_info( $info_type = 'name', $col_offset = -1 ) {
  3404 	public function get_col_info( $info_type = 'name', $col_offset = -1 ) {
  3305 		$this->load_col_info();
  3405 		$this->load_col_info();
  3306 
  3406 
  3307 		if ( $this->col_info ) {
  3407 		if ( $this->col_info ) {
  3308 			if ( $col_offset == -1 ) {
  3408 			if ( -1 === $col_offset ) {
  3309 				$i         = 0;
  3409 				$i         = 0;
  3310 				$new_array = array();
  3410 				$new_array = array();
  3311 				foreach ( (array) $this->col_info as $col ) {
  3411 				foreach ( (array) $this->col_info as $col ) {
  3312 					$new_array[ $i ] = $col->{$info_type};
  3412 					$new_array[ $i ] = $col->{$info_type};
  3313 					$i++;
  3413 					$i++;
  3334 	/**
  3434 	/**
  3335 	 * Stops the debugging timer.
  3435 	 * Stops the debugging timer.
  3336 	 *
  3436 	 *
  3337 	 * @since 1.5.0
  3437 	 * @since 1.5.0
  3338 	 *
  3438 	 *
  3339 	 * @return float Total time spent on the query, in seconds
  3439 	 * @return float Total time spent on the query, in seconds.
  3340 	 */
  3440 	 */
  3341 	public function timer_stop() {
  3441 	public function timer_stop() {
  3342 		return ( microtime( true ) - $this->time_start );
  3442 		return ( microtime( true ) - $this->time_start );
  3343 	}
  3443 	}
  3344 
  3444 
  3347 	 *
  3447 	 *
  3348 	 * Will not die if wpdb::$show_errors is false.
  3448 	 * Will not die if wpdb::$show_errors is false.
  3349 	 *
  3449 	 *
  3350 	 * @since 1.5.0
  3450 	 * @since 1.5.0
  3351 	 *
  3451 	 *
  3352 	 * @param string $message    The Error message
  3452 	 * @param string $message    The error message.
  3353 	 * @param string $error_code Optional. A Computer readable string to identify the error.
  3453 	 * @param string $error_code Optional. A computer-readable string to identify the error.
  3354 	 * @return false|void
  3454 	 *                           Default '500'.
       
  3455 	 * @return void|false Void if the showing of errors is enabled, false if disabled.
  3355 	 */
  3456 	 */
  3356 	public function bail( $message, $error_code = '500' ) {
  3457 	public function bail( $message, $error_code = '500' ) {
  3357 		if ( $this->show_errors ) {
  3458 		if ( $this->show_errors ) {
  3358 			$error = '';
  3459 			$error = '';
  3359 
  3460 
  3391 	/**
  3492 	/**
  3392 	 * Closes the current database connection.
  3493 	 * Closes the current database connection.
  3393 	 *
  3494 	 *
  3394 	 * @since 4.5.0
  3495 	 * @since 4.5.0
  3395 	 *
  3496 	 *
  3396 	 * @return bool True if the connection was successfully closed, false if it wasn't,
  3497 	 * @return bool True if the connection was successfully closed,
  3397 	 *              or the connection doesn't exist.
  3498 	 *              false if it wasn't, or if the connection doesn't exist.
  3398 	 */
  3499 	 */
  3399 	public function close() {
  3500 	public function close() {
  3400 		if ( ! $this->dbh ) {
  3501 		if ( ! $this->dbh ) {
  3401 			return false;
  3502 			return false;
  3402 		}
  3503 		}
  3415 
  3516 
  3416 		return $closed;
  3517 		return $closed;
  3417 	}
  3518 	}
  3418 
  3519 
  3419 	/**
  3520 	/**
  3420 	 * Whether MySQL database is at least the required minimum version.
  3521 	 * Determines whether MySQL database is at least the required minimum version.
  3421 	 *
  3522 	 *
  3422 	 * @since 2.5.0
  3523 	 * @since 2.5.0
  3423 	 *
  3524 	 *
  3424 	 * @global string $wp_version
  3525 	 * @global string $wp_version             The WordPress version string.
  3425 	 * @global string $required_mysql_version
  3526 	 * @global string $required_mysql_version The required MySQL version string.
  3426 	 *
  3527 	 * @return void|WP_Error
  3427 	 * @return WP_Error|void
       
  3428 	 */
  3528 	 */
  3429 	public function check_database_version() {
  3529 	public function check_database_version() {
  3430 		global $wp_version, $required_mysql_version;
  3530 		global $wp_version, $required_mysql_version;
  3431 		// Make sure the server has the required MySQL version
  3531 		// Make sure the server has the required MySQL version.
  3432 		if ( version_compare( $this->db_version(), $required_mysql_version, '<' ) ) {
  3532 		if ( version_compare( $this->db_version(), $required_mysql_version, '<' ) ) {
  3433 			/* translators: 1: WordPress version number, 2: Minimum required MySQL version number */
  3533 			/* translators: 1: WordPress version number, 2: Minimum required MySQL version number. */
  3434 			return new WP_Error( 'database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ) );
  3534 			return new WP_Error( 'database_version', sprintf( __( '<strong>Error</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ) );
  3435 		}
  3535 		}
  3436 	}
  3536 	}
  3437 
  3537 
  3438 	/**
  3538 	/**
  3439 	 * Whether the database supports collation.
  3539 	 * Determines whether the database supports collation.
  3440 	 *
  3540 	 *
  3441 	 * Called when WordPress is generating the table scheme.
  3541 	 * Called when WordPress is generating the table scheme.
  3442 	 *
  3542 	 *
  3443 	 * Use `wpdb::has_cap( 'collation' )`.
  3543 	 * Use `wpdb::has_cap( 'collation' )`.
  3444 	 *
  3544 	 *
  3445 	 * @since 2.5.0
  3545 	 * @since 2.5.0
  3446 	 * @deprecated 3.5.0 Use wpdb::has_cap()
  3546 	 * @deprecated 3.5.0 Use wpdb::has_cap()
  3447 	 *
  3547 	 *
  3448 	 * @return bool True if collation is supported, false if version does not
  3548 	 * @return bool True if collation is supported, false if not.
  3449 	 */
  3549 	 */
  3450 	public function supports_collation() {
  3550 	public function supports_collation() {
  3451 		_deprecated_function( __FUNCTION__, '3.5.0', 'wpdb::has_cap( \'collation\' )' );
  3551 		_deprecated_function( __FUNCTION__, '3.5.0', 'wpdb::has_cap( \'collation\' )' );
  3452 		return $this->has_cap( 'collation' );
  3552 		return $this->has_cap( 'collation' );
  3453 	}
  3553 	}
  3454 
  3554 
  3455 	/**
  3555 	/**
  3456 	 * The database character collate.
  3556 	 * Retrieves the database character collate.
  3457 	 *
  3557 	 *
  3458 	 * @since 3.5.0
  3558 	 * @since 3.5.0
  3459 	 *
  3559 	 *
  3460 	 * @return string The database character collate.
  3560 	 * @return string The database character collate.
  3461 	 */
  3561 	 */
  3471 
  3571 
  3472 		return $charset_collate;
  3572 		return $charset_collate;
  3473 	}
  3573 	}
  3474 
  3574 
  3475 	/**
  3575 	/**
  3476 	 * Determine if a database supports a particular feature.
  3576 	 * Determines if a database supports a particular feature.
  3477 	 *
  3577 	 *
  3478 	 * @since 2.7.0
  3578 	 * @since 2.7.0
  3479 	 * @since 4.1.0 Added support for the 'utf8mb4' feature.
  3579 	 * @since 4.1.0 Added support for the 'utf8mb4' feature.
  3480 	 * @since 4.6.0 Added support for the 'utf8mb4_520' feature.
  3580 	 * @since 4.6.0 Added support for the 'utf8mb4_520' feature.
  3481 	 *
  3581 	 *
  3482 	 * @see wpdb::db_version()
  3582 	 * @see wpdb::db_version()
  3483 	 *
  3583 	 *
  3484 	 * @param string $db_cap The feature to check for. Accepts 'collation',
  3584 	 * @param string $db_cap The feature to check for. Accepts 'collation', 'group_concat',
  3485 	 *                       'group_concat', 'subqueries', 'set_charset',
  3585 	 *                       'subqueries', 'set_charset', 'utf8mb4', or 'utf8mb4_520'.
  3486 	 *                       'utf8mb4', or 'utf8mb4_520'.
       
  3487 	 * @return int|false Whether the database feature is supported, false otherwise.
  3586 	 * @return int|false Whether the database feature is supported, false otherwise.
  3488 	 */
  3587 	 */
  3489 	public function has_cap( $db_cap ) {
  3588 	public function has_cap( $db_cap ) {
  3490 		$version = $this->db_version();
  3589 		$version = $this->db_version();
  3491 
  3590 
  3522 
  3621 
  3523 		return false;
  3622 		return false;
  3524 	}
  3623 	}
  3525 
  3624 
  3526 	/**
  3625 	/**
  3527 	 * Retrieve the name of the function that called wpdb.
  3626 	 * Retrieves the name of the function that called wpdb.
  3528 	 *
  3627 	 *
  3529 	 * Searches up the list of functions until it reaches
  3628 	 * Searches up the list of functions until it reaches the one that would
  3530 	 * the one that would most logically had called this method.
  3629 	 * most logically had called this method.
  3531 	 *
  3630 	 *
  3532 	 * @since 2.5.0
  3631 	 * @since 2.5.0
  3533 	 *
  3632 	 *
  3534 	 * @return string Comma separated list of the calling functions.
  3633 	 * @return string Comma-separated list of the calling functions.
  3535 	 */
  3634 	 */
  3536 	public function get_caller() {
  3635 	public function get_caller() {
  3537 		return wp_debug_backtrace_summary( __CLASS__ );
  3636 		return wp_debug_backtrace_summary( __CLASS__ );
  3538 	}
  3637 	}
  3539 
  3638 
  3540 	/**
  3639 	/**
  3541 	 * Retrieves the MySQL server version.
  3640 	 * Retrieves the MySQL server version.
  3542 	 *
  3641 	 *
  3543 	 * @since 2.7.0
  3642 	 * @since 2.7.0
  3544 	 *
  3643 	 *
  3545 	 * @return null|string Null on failure, version number on success.
  3644 	 * @return string|null Version number on success, null on failure.
  3546 	 */
  3645 	 */
  3547 	public function db_version() {
  3646 	public function db_version() {
       
  3647 		return preg_replace( '/[^0-9.].*/', '', $this->db_server_info() );
       
  3648 	}
       
  3649 
       
  3650 	/**
       
  3651 	 * Retrieves full MySQL server information.
       
  3652 	 *
       
  3653 	 * @since 5.5.0
       
  3654 	 *
       
  3655 	 * @return string|false Server info on success, false on failure.
       
  3656 	 */
       
  3657 	public function db_server_info() {
  3548 		if ( $this->use_mysqli ) {
  3658 		if ( $this->use_mysqli ) {
  3549 			$server_info = mysqli_get_server_info( $this->dbh );
  3659 			$server_info = mysqli_get_server_info( $this->dbh );
  3550 		} else {
  3660 		} else {
  3551 			$server_info = mysql_get_server_info( $this->dbh );
  3661 			$server_info = mysql_get_server_info( $this->dbh );
  3552 		}
  3662 		}
  3553 		return preg_replace( '/[^0-9.].*/', '', $server_info );
  3663 
       
  3664 		return $server_info;
  3554 	}
  3665 	}
  3555 }
  3666 }