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; |
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 = ''; |
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() |
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; |
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': |
1100 wp_load_translations_early(); |
1091 wp_load_translations_early(); |
1101 |
1092 |
1102 $message = '<h1>' . __( 'Can’t select database' ) . "</h1>\n"; |
1093 $message = '<h1>' . __( 'Can’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’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’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' ); |
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, |
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’t contact the database server at %2$s. This could mean your host’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’t contact the database server at %2$s. This could mean your host’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’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’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. |
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’s database server is down.' ), |
1846 __( 'This means that we lost contact with the database server at %s. This could mean your host’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’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’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 ) { |
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 ) ) { |
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 * |
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. |
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; |
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; |
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 ) { |
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 ); |
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 ) { |
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 } |
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. |
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++; |
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 } |