57 * Whether to show SQL/DB errors. |
57 * Whether to show SQL/DB errors. |
58 * |
58 * |
59 * Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true. |
59 * Default is to show errors if both WP_DEBUG and WP_DEBUG_DISPLAY evaluate to true. |
60 * |
60 * |
61 * @since 0.71 |
61 * @since 0.71 |
|
62 * |
62 * @var bool |
63 * @var bool |
63 */ |
64 */ |
64 public $show_errors = false; |
65 public $show_errors = false; |
65 |
66 |
66 /** |
67 /** |
67 * Whether to suppress errors during the DB bootstrapping. Default false. |
68 * Whether to suppress errors during the DB bootstrapping. Default false. |
68 * |
69 * |
69 * @since 2.5.0 |
70 * @since 2.5.0 |
|
71 * |
70 * @var bool |
72 * @var bool |
71 */ |
73 */ |
72 public $suppress_errors = false; |
74 public $suppress_errors = false; |
73 |
75 |
74 /** |
76 /** |
75 * The error encountered during the last query. |
77 * The error encountered during the last query. |
76 * |
78 * |
77 * @since 2.5.0 |
79 * @since 2.5.0 |
|
80 * |
78 * @var string |
81 * @var string |
79 */ |
82 */ |
80 public $last_error = ''; |
83 public $last_error = ''; |
81 |
84 |
82 /** |
85 /** |
83 * The number of queries made. |
86 * The number of queries made. |
84 * |
87 * |
85 * @since 1.2.0 |
88 * @since 1.2.0 |
|
89 * |
86 * @var int |
90 * @var int |
87 */ |
91 */ |
88 public $num_queries = 0; |
92 public $num_queries = 0; |
89 |
93 |
90 /** |
94 /** |
91 * Count of rows returned by the last query. |
95 * Count of rows returned by the last query. |
92 * |
96 * |
93 * @since 0.71 |
97 * @since 0.71 |
|
98 * |
94 * @var int |
99 * @var int |
95 */ |
100 */ |
96 public $num_rows = 0; |
101 public $num_rows = 0; |
97 |
102 |
98 /** |
103 /** |
99 * Count of rows affected by the last query. |
104 * Count of rows affected by the last query. |
100 * |
105 * |
101 * @since 0.71 |
106 * @since 0.71 |
|
107 * |
102 * @var int |
108 * @var int |
103 */ |
109 */ |
104 public $rows_affected = 0; |
110 public $rows_affected = 0; |
105 |
111 |
106 /** |
112 /** |
107 * The ID generated for an AUTO_INCREMENT column by the last query (usually INSERT). |
113 * The ID generated for an AUTO_INCREMENT column by the last query (usually INSERT). |
108 * |
114 * |
109 * @since 0.71 |
115 * @since 0.71 |
|
116 * |
110 * @var int |
117 * @var int |
111 */ |
118 */ |
112 public $insert_id = 0; |
119 public $insert_id = 0; |
113 |
120 |
114 /** |
121 /** |
115 * The last query made. |
122 * The last query made. |
116 * |
123 * |
117 * @since 0.71 |
124 * @since 0.71 |
|
125 * |
118 * @var string |
126 * @var string |
119 */ |
127 */ |
120 public $last_query; |
128 public $last_query; |
121 |
129 |
122 /** |
130 /** |
123 * Results of the last query. |
131 * Results of the last query. |
124 * |
132 * |
125 * @since 0.71 |
133 * @since 0.71 |
126 * @var array|null |
134 * |
|
135 * @var stdClass[]|null |
127 */ |
136 */ |
128 public $last_result; |
137 public $last_result; |
129 |
138 |
130 /** |
139 /** |
131 * MySQL result, which is either a resource or boolean. |
140 * Database query result. |
|
141 * |
|
142 * Possible values: |
|
143 * |
|
144 * - For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries: |
|
145 * - `mysqli_result` instance when the `mysqli` driver is in use |
|
146 * - `resource` when the older `mysql` driver is in use |
|
147 * - `true` for other query types that were successful |
|
148 * - `null` if a query is yet to be made or if the result has since been flushed |
|
149 * - `false` if the query returned an error |
132 * |
150 * |
133 * @since 0.71 |
151 * @since 0.71 |
134 * @var mixed |
152 * |
|
153 * @var mysqli_result|resource|bool|null |
135 */ |
154 */ |
136 protected $result; |
155 protected $result; |
137 |
156 |
138 /** |
157 /** |
139 * Cached column info, for sanity checking data before inserting. |
158 * Cached column info, for sanity checking data before inserting. |
140 * |
159 * |
141 * @since 4.2.0 |
160 * @since 4.2.0 |
|
161 * |
142 * @var array |
162 * @var array |
143 */ |
163 */ |
144 protected $col_meta = array(); |
164 protected $col_meta = array(); |
145 |
165 |
146 /** |
166 /** |
147 * Calculated character sets on tables. |
167 * Calculated character sets keyed by table name. |
148 * |
168 * |
149 * @since 4.2.0 |
169 * @since 4.2.0 |
150 * @var array |
170 * |
|
171 * @var string[] |
151 */ |
172 */ |
152 protected $table_charset = array(); |
173 protected $table_charset = array(); |
153 |
174 |
154 /** |
175 /** |
155 * Whether text fields in the current query need to be sanity checked. Default false. |
176 * Whether text fields in the current query need to be sanity checked. |
156 * |
177 * |
157 * @since 4.2.0 |
178 * @since 4.2.0 |
|
179 * |
158 * @var bool |
180 * @var bool |
159 */ |
181 */ |
160 protected $check_current_query = true; |
182 protected $check_current_query = true; |
161 |
183 |
162 /** |
184 /** |
163 * Flag to ensure we don't run into recursion problems when checking the collation. |
185 * Flag to ensure we don't run into recursion problems when checking the collation. |
164 * |
186 * |
165 * @since 4.2.0 |
187 * @since 4.2.0 |
|
188 * |
166 * @see wpdb::check_safe_collation() |
189 * @see wpdb::check_safe_collation() |
167 * @var bool |
190 * @var bool |
168 */ |
191 */ |
169 private $checking_collation = false; |
192 private $checking_collation = false; |
170 |
193 |
171 /** |
194 /** |
172 * Saved info on the table column. |
195 * Saved info on the table column. |
173 * |
196 * |
174 * @since 0.71 |
197 * @since 0.71 |
|
198 * |
175 * @var array |
199 * @var array |
176 */ |
200 */ |
177 protected $col_info; |
201 protected $col_info; |
178 |
202 |
179 /** |
203 /** |
183 * @since 2.5.0 The third element in each query log was added to record the calling functions. |
207 * @since 2.5.0 The third element in each query log was added to record the calling functions. |
184 * @since 5.1.0 The fourth element in each query log was added to record the start time. |
208 * @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. |
209 * @since 5.3.0 The fifth element in each query log was added to record custom data. |
186 * |
210 * |
187 * @var array[] { |
211 * @var array[] { |
188 * Array of queries that were executed. |
212 * Array of arrays containing information about queries that were executed. |
189 * |
213 * |
190 * @type array ...$0 { |
214 * @type array ...$0 { |
191 * Data for each query. |
215 * Data for each query. |
192 * |
216 * |
193 * @type string $0 The query's SQL. |
217 * @type string $0 The query's SQL. |
202 |
226 |
203 /** |
227 /** |
204 * The number of times to retry reconnecting before dying. Default 5. |
228 * The number of times to retry reconnecting before dying. Default 5. |
205 * |
229 * |
206 * @since 3.9.0 |
230 * @since 3.9.0 |
|
231 * |
207 * @see wpdb::check_connection() |
232 * @see wpdb::check_connection() |
208 * @var int |
233 * @var int |
209 */ |
234 */ |
210 protected $reconnect_retries = 5; |
235 protected $reconnect_retries = 5; |
211 |
236 |
212 /** |
237 /** |
213 * WordPress table prefix |
238 * WordPress table prefix. |
214 * |
239 * |
215 * You can set this to have multiple WordPress installations in a single database. |
240 * You can set this to have multiple WordPress installations in a single database. |
216 * The second reason is for possible security precautions. |
241 * The second reason is for possible security precautions. |
217 * |
242 * |
218 * @since 2.5.0 |
243 * @since 2.5.0 |
|
244 * |
219 * @var string |
245 * @var string |
220 */ |
246 */ |
221 public $prefix = ''; |
247 public $prefix = ''; |
222 |
248 |
223 /** |
249 /** |
224 * WordPress base table prefix. |
250 * WordPress base table prefix. |
225 * |
251 * |
226 * @since 3.0.0 |
252 * @since 3.0.0 |
|
253 * |
227 * @var string |
254 * @var string |
228 */ |
255 */ |
229 public $base_prefix; |
256 public $base_prefix; |
230 |
257 |
231 /** |
258 /** |
232 * Whether the database queries are ready to start executing. |
259 * Whether the database queries are ready to start executing. |
233 * |
260 * |
234 * @since 2.3.2 |
261 * @since 2.3.2 |
|
262 * |
235 * @var bool |
263 * @var bool |
236 */ |
264 */ |
237 public $ready = false; |
265 public $ready = false; |
238 |
266 |
239 /** |
267 /** |
240 * Blog ID. |
268 * Blog ID. |
241 * |
269 * |
242 * @since 3.0.0 |
270 * @since 3.0.0 |
|
271 * |
243 * @var int |
272 * @var int |
244 */ |
273 */ |
245 public $blogid = 0; |
274 public $blogid = 0; |
246 |
275 |
247 /** |
276 /** |
248 * Site ID. |
277 * Site ID. |
249 * |
278 * |
250 * @since 3.0.0 |
279 * @since 3.0.0 |
|
280 * |
251 * @var int |
281 * @var int |
252 */ |
282 */ |
253 public $siteid = 0; |
283 public $siteid = 0; |
254 |
284 |
255 /** |
285 /** |
256 * List of WordPress per-blog tables. |
286 * List of WordPress per-site tables. |
257 * |
287 * |
258 * @since 2.5.0 |
288 * @since 2.5.0 |
|
289 * |
259 * @see wpdb::tables() |
290 * @see wpdb::tables() |
260 * @var array |
291 * @var string[] |
261 */ |
292 */ |
262 public $tables = array( |
293 public $tables = array( |
263 'posts', |
294 'posts', |
264 'comments', |
295 'comments', |
265 'links', |
296 'links', |
276 * List of deprecated WordPress tables. |
307 * List of deprecated WordPress tables. |
277 * |
308 * |
278 * 'categories', 'post2cat', and 'link2cat' were deprecated in 2.3.0, db version 5539. |
309 * 'categories', 'post2cat', and 'link2cat' were deprecated in 2.3.0, db version 5539. |
279 * |
310 * |
280 * @since 2.9.0 |
311 * @since 2.9.0 |
|
312 * |
281 * @see wpdb::tables() |
313 * @see wpdb::tables() |
282 * @var array |
314 * @var string[] |
283 */ |
315 */ |
284 public $old_tables = array( 'categories', 'post2cat', 'link2cat' ); |
316 public $old_tables = array( 'categories', 'post2cat', 'link2cat' ); |
285 |
317 |
286 /** |
318 /** |
287 * List of WordPress global tables. |
319 * List of WordPress global tables. |
288 * |
320 * |
289 * @since 3.0.0 |
321 * @since 3.0.0 |
|
322 * |
290 * @see wpdb::tables() |
323 * @see wpdb::tables() |
291 * @var array |
324 * @var string[] |
292 */ |
325 */ |
293 public $global_tables = array( 'users', 'usermeta' ); |
326 public $global_tables = array( 'users', 'usermeta' ); |
294 |
327 |
295 /** |
328 /** |
296 * List of Multisite global tables. |
329 * List of Multisite global tables. |
297 * |
330 * |
298 * @since 3.0.0 |
331 * @since 3.0.0 |
|
332 * |
299 * @see wpdb::tables() |
333 * @see wpdb::tables() |
300 * @var array |
334 * @var string[] |
301 */ |
335 */ |
302 public $ms_global_tables = array( |
336 public $ms_global_tables = array( |
303 'blogs', |
337 'blogs', |
304 'blogmeta', |
338 'blogmeta', |
305 'signups', |
339 'signups', |
311 |
345 |
312 /** |
346 /** |
313 * WordPress Comments table. |
347 * WordPress Comments table. |
314 * |
348 * |
315 * @since 1.5.0 |
349 * @since 1.5.0 |
|
350 * |
316 * @var string |
351 * @var string |
317 */ |
352 */ |
318 public $comments; |
353 public $comments; |
319 |
354 |
320 /** |
355 /** |
321 * WordPress Comment Metadata table. |
356 * WordPress Comment Metadata table. |
322 * |
357 * |
323 * @since 2.9.0 |
358 * @since 2.9.0 |
|
359 * |
324 * @var string |
360 * @var string |
325 */ |
361 */ |
326 public $commentmeta; |
362 public $commentmeta; |
327 |
363 |
328 /** |
364 /** |
329 * WordPress Links table. |
365 * WordPress Links table. |
330 * |
366 * |
331 * @since 1.5.0 |
367 * @since 1.5.0 |
|
368 * |
332 * @var string |
369 * @var string |
333 */ |
370 */ |
334 public $links; |
371 public $links; |
335 |
372 |
336 /** |
373 /** |
337 * WordPress Options table. |
374 * WordPress Options table. |
338 * |
375 * |
339 * @since 1.5.0 |
376 * @since 1.5.0 |
|
377 * |
340 * @var string |
378 * @var string |
341 */ |
379 */ |
342 public $options; |
380 public $options; |
343 |
381 |
344 /** |
382 /** |
345 * WordPress Post Metadata table. |
383 * WordPress Post Metadata table. |
346 * |
384 * |
347 * @since 1.5.0 |
385 * @since 1.5.0 |
|
386 * |
348 * @var string |
387 * @var string |
349 */ |
388 */ |
350 public $postmeta; |
389 public $postmeta; |
351 |
390 |
352 /** |
391 /** |
353 * WordPress Posts table. |
392 * WordPress Posts table. |
354 * |
393 * |
355 * @since 1.5.0 |
394 * @since 1.5.0 |
|
395 * |
356 * @var string |
396 * @var string |
357 */ |
397 */ |
358 public $posts; |
398 public $posts; |
359 |
399 |
360 /** |
400 /** |
361 * WordPress Terms table. |
401 * WordPress Terms table. |
362 * |
402 * |
363 * @since 2.3.0 |
403 * @since 2.3.0 |
|
404 * |
364 * @var string |
405 * @var string |
365 */ |
406 */ |
366 public $terms; |
407 public $terms; |
367 |
408 |
368 /** |
409 /** |
369 * WordPress Term Relationships table. |
410 * WordPress Term Relationships table. |
370 * |
411 * |
371 * @since 2.3.0 |
412 * @since 2.3.0 |
|
413 * |
372 * @var string |
414 * @var string |
373 */ |
415 */ |
374 public $term_relationships; |
416 public $term_relationships; |
375 |
417 |
376 /** |
418 /** |
377 * WordPress Term Taxonomy table. |
419 * WordPress Term Taxonomy table. |
378 * |
420 * |
379 * @since 2.3.0 |
421 * @since 2.3.0 |
|
422 * |
380 * @var string |
423 * @var string |
381 */ |
424 */ |
382 public $term_taxonomy; |
425 public $term_taxonomy; |
383 |
426 |
384 /** |
427 /** |
385 * WordPress Term Meta table. |
428 * WordPress Term Meta table. |
386 * |
429 * |
387 * @since 4.4.0 |
430 * @since 4.4.0 |
|
431 * |
388 * @var string |
432 * @var string |
389 */ |
433 */ |
390 public $termmeta; |
434 public $termmeta; |
391 |
435 |
392 // |
436 // |
395 |
439 |
396 /** |
440 /** |
397 * WordPress User Metadata table. |
441 * WordPress User Metadata table. |
398 * |
442 * |
399 * @since 2.3.0 |
443 * @since 2.3.0 |
|
444 * |
400 * @var string |
445 * @var string |
401 */ |
446 */ |
402 public $usermeta; |
447 public $usermeta; |
403 |
448 |
404 /** |
449 /** |
405 * WordPress Users table. |
450 * WordPress Users table. |
406 * |
451 * |
407 * @since 1.5.0 |
452 * @since 1.5.0 |
|
453 * |
408 * @var string |
454 * @var string |
409 */ |
455 */ |
410 public $users; |
456 public $users; |
411 |
457 |
412 /** |
458 /** |
413 * Multisite Blogs table. |
459 * Multisite Blogs table. |
414 * |
460 * |
415 * @since 3.0.0 |
461 * @since 3.0.0 |
|
462 * |
416 * @var string |
463 * @var string |
417 */ |
464 */ |
418 public $blogs; |
465 public $blogs; |
419 |
466 |
420 /** |
467 /** |
421 * Multisite Blog Metadata table. |
468 * Multisite Blog Metadata table. |
422 * |
469 * |
423 * @since 5.1.0 |
470 * @since 5.1.0 |
|
471 * |
424 * @var string |
472 * @var string |
425 */ |
473 */ |
426 public $blogmeta; |
474 public $blogmeta; |
427 |
475 |
428 /** |
476 /** |
429 * Multisite Registration Log table. |
477 * Multisite Registration Log table. |
430 * |
478 * |
431 * @since 3.0.0 |
479 * @since 3.0.0 |
|
480 * |
432 * @var string |
481 * @var string |
433 */ |
482 */ |
434 public $registration_log; |
483 public $registration_log; |
435 |
484 |
436 /** |
485 /** |
437 * Multisite Signups table. |
486 * Multisite Signups table. |
438 * |
487 * |
439 * @since 3.0.0 |
488 * @since 3.0.0 |
|
489 * |
440 * @var string |
490 * @var string |
441 */ |
491 */ |
442 public $signups; |
492 public $signups; |
443 |
493 |
444 /** |
494 /** |
445 * Multisite Sites table. |
495 * Multisite Sites table. |
446 * |
496 * |
447 * @since 3.0.0 |
497 * @since 3.0.0 |
|
498 * |
448 * @var string |
499 * @var string |
449 */ |
500 */ |
450 public $site; |
501 public $site; |
451 |
502 |
452 /** |
503 /** |
453 * Multisite Sitewide Terms table. |
504 * Multisite Sitewide Terms table. |
454 * |
505 * |
455 * @since 3.0.0 |
506 * @since 3.0.0 |
|
507 * |
456 * @var string |
508 * @var string |
457 */ |
509 */ |
458 public $sitecategories; |
510 public $sitecategories; |
459 |
511 |
460 /** |
512 /** |
461 * Multisite Site Metadata table. |
513 * Multisite Site Metadata table. |
462 * |
514 * |
463 * @since 3.0.0 |
515 * @since 3.0.0 |
|
516 * |
464 * @var string |
517 * @var string |
465 */ |
518 */ |
466 public $sitemeta; |
519 public $sitemeta; |
467 |
520 |
468 /** |
521 /** |
470 * |
523 * |
471 * Columns not listed here default to %s. Initialized during WP load. |
524 * Columns not listed here default to %s. Initialized during WP load. |
472 * Keys are column names, values are format types: 'ID' => '%d'. |
525 * Keys are column names, values are format types: 'ID' => '%d'. |
473 * |
526 * |
474 * @since 2.8.0 |
527 * @since 2.8.0 |
|
528 * |
475 * @see wpdb::prepare() |
529 * @see wpdb::prepare() |
476 * @see wpdb::insert() |
530 * @see wpdb::insert() |
477 * @see wpdb::update() |
531 * @see wpdb::update() |
478 * @see wpdb::delete() |
532 * @see wpdb::delete() |
479 * @see wp_set_wpdb_vars() |
533 * @see wp_set_wpdb_vars() |
552 * against the required MySQL version for WordPress. Normally, a replacement |
621 * against the required MySQL version for WordPress. Normally, a replacement |
553 * database drop-in (db.php) will skip these checks, but setting this to true |
622 * database drop-in (db.php) will skip these checks, but setting this to true |
554 * will force the checks to occur. |
623 * will force the checks to occur. |
555 * |
624 * |
556 * @since 3.3.0 |
625 * @since 3.3.0 |
|
626 * |
557 * @var bool |
627 * @var bool |
558 */ |
628 */ |
559 public $is_mysql = null; |
629 public $is_mysql = null; |
560 |
630 |
561 /** |
631 /** |
562 * A list of incompatible SQL modes. |
632 * A list of incompatible SQL modes. |
563 * |
633 * |
564 * @since 3.9.0 |
634 * @since 3.9.0 |
565 * @var array |
635 * |
|
636 * @var string[] |
566 */ |
637 */ |
567 protected $incompatible_modes = array( |
638 protected $incompatible_modes = array( |
568 'NO_ZERO_DATE', |
639 'NO_ZERO_DATE', |
569 'ONLY_FULL_GROUP_BY', |
640 'ONLY_FULL_GROUP_BY', |
570 'STRICT_TRANS_TABLES', |
641 'STRICT_TRANS_TABLES', |
575 |
646 |
576 /** |
647 /** |
577 * Whether to use mysqli over mysql. Default false. |
648 * Whether to use mysqli over mysql. Default false. |
578 * |
649 * |
579 * @since 3.9.0 |
650 * @since 3.9.0 |
|
651 * |
580 * @var bool |
652 * @var bool |
581 */ |
653 */ |
582 private $use_mysqli = false; |
654 private $use_mysqli = false; |
583 |
655 |
584 /** |
656 /** |
585 * Whether we've managed to successfully connect at some point. |
657 * Whether we've managed to successfully connect at some point. |
586 * |
658 * |
587 * @since 3.9.0 |
659 * @since 3.9.0 |
|
660 * |
588 * @var bool |
661 * @var bool |
589 */ |
662 */ |
590 private $has_connected = false; |
663 private $has_connected = false; |
591 |
664 |
592 /** |
665 /** |
|
666 * Time when the last query was performed. |
|
667 * |
|
668 * Only set when `SAVEQUERIES` is defined and truthy. |
|
669 * |
|
670 * @since 1.5.0 |
|
671 * |
|
672 * @var float |
|
673 */ |
|
674 public $time_start = null; |
|
675 |
|
676 /** |
|
677 * The last SQL error that was encountered. |
|
678 * |
|
679 * @since 2.5.0 |
|
680 * |
|
681 * @var WP_Error|string |
|
682 */ |
|
683 public $error = null; |
|
684 |
|
685 /** |
593 * Connects to the database server and selects a database. |
686 * Connects to the database server and selects a database. |
594 * |
687 * |
595 * PHP5 style constructor for compatibility with PHP5. Does the actual setting up |
688 * Does the actual setting up |
596 * of the class properties and connection to the database. |
689 * of the class properties and connection to the database. |
597 * |
690 * |
598 * @since 2.0.8 |
691 * @since 2.0.8 |
599 * |
692 * |
600 * @link https://core.trac.wordpress.org/ticket/3354 |
693 * @link https://core.trac.wordpress.org/ticket/3354 |
601 * @global string $wp_version The WordPress version string. |
694 * |
602 * |
695 * @param string $dbuser Database user. |
603 * @param string $dbuser MySQL database user. |
696 * @param string $dbpassword Database password. |
604 * @param string $dbpassword MySQL database password. |
697 * @param string $dbname Database name. |
605 * @param string $dbname MySQL database name. |
698 * @param string $dbhost Database host. |
606 * @param string $dbhost MySQL database host. |
|
607 */ |
699 */ |
608 public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
700 public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
609 if ( WP_DEBUG && WP_DEBUG_DISPLAY ) { |
701 if ( WP_DEBUG && WP_DEBUG_DISPLAY ) { |
610 $this->show_errors(); |
702 $this->show_errors(); |
611 } |
703 } |
612 |
704 |
613 // Use ext/mysqli if it exists unless WP_USE_EXT_MYSQL is defined as true. |
705 // Use the `mysqli` extension if it exists unless `WP_USE_EXT_MYSQL` is defined as true. |
614 if ( function_exists( 'mysqli_connect' ) ) { |
706 if ( function_exists( 'mysqli_connect' ) ) { |
615 $this->use_mysqli = true; |
707 $this->use_mysqli = true; |
616 |
708 |
617 if ( defined( 'WP_USE_EXT_MYSQL' ) ) { |
709 if ( defined( 'WP_USE_EXT_MYSQL' ) ) { |
618 $this->use_mysqli = ! WP_USE_EXT_MYSQL; |
710 $this->use_mysqli = ! WP_USE_EXT_MYSQL; |
771 /** |
863 /** |
772 * Sets the connection's character set. |
864 * Sets the connection's character set. |
773 * |
865 * |
774 * @since 3.1.0 |
866 * @since 3.1.0 |
775 * |
867 * |
776 * @param resource $dbh The resource given by mysql_connect. |
868 * @param mysqli|resource $dbh The connection returned by `mysqli_connect()` or `mysql_connect()`. |
777 * @param string $charset Optional. The character set. Default null. |
869 * @param string $charset Optional. The character set. Default null. |
778 * @param string $collate Optional. The collation. Default null. |
870 * @param string $collate Optional. The collation. Default null. |
779 */ |
871 */ |
780 public function set_charset( $dbh, $charset = null, $collate = null ) { |
872 public function set_charset( $dbh, $charset = null, $collate = null ) { |
781 if ( ! isset( $charset ) ) { |
873 if ( ! isset( $charset ) ) { |
782 $charset = $this->charset; |
874 $charset = $this->charset; |
783 } |
875 } |
981 } |
1073 } |
982 |
1074 |
983 /** |
1075 /** |
984 * Returns an array of WordPress tables. |
1076 * Returns an array of WordPress tables. |
985 * |
1077 * |
986 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to override the WordPress users |
1078 * Also allows for the `CUSTOM_USER_TABLE` and `CUSTOM_USER_META_TABLE` to override the WordPress users |
987 * and usermeta tables that would otherwise be determined by the prefix. |
1079 * and usermeta tables that would otherwise be determined by the prefix. |
988 * |
1080 * |
989 * The $scope argument can take one of the following: |
1081 * The `$scope` argument can take one of the following: |
990 * |
1082 * |
991 * 'all' - returns 'all' and 'global' tables. No old tables are returned. |
1083 * - 'all' - returns 'all' and 'global' tables. No old tables are returned. |
992 * 'blog' - returns the blog-level tables for the queried blog. |
1084 * - 'blog' - returns the blog-level tables for the queried blog. |
993 * 'global' - returns the global tables for the installation, returning multisite tables only on multisite. |
1085 * - 'global' - returns the global tables for the installation, returning multisite tables only on multisite. |
994 * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. |
1086 * - 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. |
995 * 'old' - returns tables which are deprecated. |
1087 * - 'old' - returns tables which are deprecated. |
996 * |
1088 * |
997 * @since 3.0.0 |
1089 * @since 3.0.0 |
998 * |
1090 * |
999 * @uses wpdb::$tables |
1091 * @uses wpdb::$tables |
1000 * @uses wpdb::$old_tables |
1092 * @uses wpdb::$old_tables |
1004 * @param string $scope Optional. Possible values include 'all', 'global', 'ms_global', 'blog', |
1096 * @param string $scope Optional. Possible values include 'all', 'global', 'ms_global', 'blog', |
1005 * or 'old' tables. Default 'all'. |
1097 * or 'old' tables. Default 'all'. |
1006 * @param bool $prefix Optional. Whether to include table prefixes. If blog prefix is requested, |
1098 * @param bool $prefix Optional. Whether to include table prefixes. If blog prefix is requested, |
1007 * then the custom users and usermeta tables will be mapped. Default true. |
1099 * 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. |
1100 * @param int $blog_id Optional. The blog_id to prefix. Used only when prefix is requested. |
1009 * Defaults to wpdb::$blogid. |
1101 * Defaults to `wpdb::$blogid`. |
1010 * @return array Table names. When a prefix is requested, the key is the unprefixed table name. |
1102 * @return string[] Table names. When a prefix is requested, the key is the unprefixed table name. |
1011 */ |
1103 */ |
1012 public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { |
1104 public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { |
1013 switch ( $scope ) { |
1105 switch ( $scope ) { |
1014 case 'all': |
1106 case 'all': |
1015 $tables = array_merge( $this->global_tables, $this->tables ); |
1107 $tables = array_merge( $this->global_tables, $this->tables ); |
1063 |
1155 |
1064 return $tables; |
1156 return $tables; |
1065 } |
1157 } |
1066 |
1158 |
1067 /** |
1159 /** |
1068 * Selects a database using the current database connection. |
1160 * Selects a database using the current or provided database connection. |
1069 * |
1161 * |
1070 * The database name will be changed based on the current database connection. |
1162 * The database name will be changed based on the current database connection. |
1071 * On failure, the execution will bail and display a DB error. |
1163 * On failure, the execution will bail and display a DB error. |
1072 * |
1164 * |
1073 * @since 0.71 |
1165 * @since 0.71 |
1074 * |
1166 * |
1075 * @param string $db MySQL database name. |
1167 * @param string $db Database name. |
1076 * @param resource|null $dbh Optional link identifier. |
1168 * @param mysqli|resource $dbh Optional database connection. |
1077 */ |
1169 */ |
1078 public function select( $db, $dbh = null ) { |
1170 public function select( $db, $dbh = null ) { |
1079 if ( is_null( $dbh ) ) { |
1171 if ( is_null( $dbh ) ) { |
1080 $dbh = $this->dbh; |
1172 $dbh = $this->dbh; |
1081 } |
1173 } |
1088 if ( ! $success ) { |
1180 if ( ! $success ) { |
1089 $this->ready = false; |
1181 $this->ready = false; |
1090 if ( ! did_action( 'template_redirect' ) ) { |
1182 if ( ! did_action( 'template_redirect' ) ) { |
1091 wp_load_translations_early(); |
1183 wp_load_translations_early(); |
1092 |
1184 |
1093 $message = '<h1>' . __( 'Can’t select database' ) . "</h1>\n"; |
1185 $message = '<h1>' . __( 'Cannot select database' ) . "</h1>\n"; |
1094 |
1186 |
1095 $message .= '<p>' . sprintf( |
1187 $message .= '<p>' . sprintf( |
1096 /* translators: %s: Database name. */ |
1188 /* translators: %s: Database name. */ |
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.' ), |
1189 __( 'The database server could be connected to (which means your username and password is okay) but the %s database could not be selected.' ), |
1098 '<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>' |
1190 '<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>' |
1099 ) . "</p>\n"; |
1191 ) . "</p>\n"; |
1100 |
1192 |
1101 $message .= "<ul>\n"; |
1193 $message .= "<ul>\n"; |
1102 $message .= '<li>' . __( 'Are you sure it exists?' ) . "</li>\n"; |
1194 $message .= '<li>' . __( 'Are you sure it exists?' ) . "</li>\n"; |
1116 |
1208 |
1117 $message .= "</ul>\n"; |
1209 $message .= "</ul>\n"; |
1118 |
1210 |
1119 $message .= '<p>' . sprintf( |
1211 $message .= '<p>' . sprintf( |
1120 /* translators: %s: Support forums URL. */ |
1212 /* translators: %s: Support forums URL. */ |
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>.' ), |
1213 __( 'If you do not 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>.' ), |
1122 __( 'https://wordpress.org/support/forums/' ) |
1214 __( 'https://wordpress.org/support/forums/' ) |
1123 ) . "</p>\n"; |
1215 ) . "</p>\n"; |
1124 |
1216 |
1125 $this->bail( $message, 'db_select_fail' ); |
1217 $this->bail( $message, 'db_select_fail' ); |
1126 } |
1218 } |
1169 } else { |
1261 } else { |
1170 $escaped = mysql_real_escape_string( $string, $this->dbh ); |
1262 $escaped = mysql_real_escape_string( $string, $this->dbh ); |
1171 } |
1263 } |
1172 } else { |
1264 } else { |
1173 $class = get_class( $this ); |
1265 $class = get_class( $this ); |
1174 if ( function_exists( '__' ) ) { |
1266 |
1175 /* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */ |
1267 wp_load_translations_early(); |
1176 _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); |
1268 /* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */ |
1177 } else { |
1269 _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); |
1178 _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' ); |
1270 |
1179 } |
|
1180 $escaped = addslashes( $string ); |
1271 $escaped = addslashes( $string ); |
1181 } |
1272 } |
1182 |
1273 |
1183 return $this->add_placeholder_escape( $escaped ); |
1274 return $this->add_placeholder_escape( $escaped ); |
1184 } |
1275 } |
1258 |
1349 |
1259 /** |
1350 /** |
1260 * Prepares a SQL query for safe execution. |
1351 * Prepares a SQL query for safe execution. |
1261 * |
1352 * |
1262 * Uses sprintf()-like syntax. The following placeholders can be used in the query string: |
1353 * Uses sprintf()-like syntax. The following placeholders can be used in the query string: |
1263 * %d (integer) |
1354 * |
1264 * %f (float) |
1355 * - %d (integer) |
1265 * %s (string) |
1356 * - %f (float) |
|
1357 * - %s (string) |
1266 * |
1358 * |
1267 * All placeholders MUST be left unquoted in the query string. A corresponding argument |
1359 * All placeholders MUST be left unquoted in the query string. A corresponding argument |
1268 * MUST be passed for each placeholder. |
1360 * MUST be passed for each placeholder. |
1269 * |
1361 * |
1270 * Note: There is one exception to the above: for compatibility with old behavior, |
1362 * Note: There is one exception to the above: for compatibility with old behavior, |
1271 * numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes |
1363 * numbered or formatted string placeholders (eg, `%1$s`, `%5s`) will not have quotes |
1272 * added by this function, so should be passed with appropriate quotes around them. |
1364 * added by this function, so should be passed with appropriate quotes around them. |
1273 * |
1365 * |
1274 * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards |
1366 * Literal percentage signs (`%`) in the query string must be written as `%%`. Percentage wildcards |
1275 * (for example, to use in LIKE syntax) must be passed via a substitution argument containing |
1367 * (for example, to use in LIKE syntax) must be passed via a substitution argument containing |
1276 * the complete LIKE string, these cannot be inserted directly in the query string. |
1368 * the complete LIKE string, these cannot be inserted directly in the query string. |
1277 * Also see wpdb::esc_like(). |
1369 * Also see wpdb::esc_like(). |
1278 * |
1370 * |
1279 * Arguments may be passed as individual arguments to the method, or as a single array |
1371 * Arguments may be passed as individual arguments to the method, or as a single array |
1280 * containing all arguments. A combination of the two is not supported. |
1372 * containing all arguments. A combination of the two is not supported. |
1281 * |
1373 * |
1282 * Examples: |
1374 * Examples: |
|
1375 * |
1283 * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); |
1376 * $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' ); |
1377 * $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); |
1285 * |
1378 * |
1286 * @since 2.3.0 |
1379 * @since 2.3.0 |
1287 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
1380 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
1626 */ |
1719 */ |
1627 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
1720 $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
1628 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
1721 $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
1629 |
1722 |
1630 if ( $this->use_mysqli ) { |
1723 if ( $this->use_mysqli ) { |
|
1724 /* |
|
1725 * Set the MySQLi error reporting off because WordPress handles its own. |
|
1726 * This is due to the default value change from `MYSQLI_REPORT_OFF` |
|
1727 * to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1. |
|
1728 */ |
|
1729 mysqli_report( MYSQLI_REPORT_OFF ); |
|
1730 |
1631 $this->dbh = mysqli_init(); |
1731 $this->dbh = mysqli_init(); |
1632 |
1732 |
1633 $host = $this->dbhost; |
1733 $host = $this->dbhost; |
1634 $port = null; |
1734 $port = null; |
1635 $socket = null; |
1735 $socket = null; |
1700 |
1800 |
1701 $message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n"; |
1801 $message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n"; |
1702 |
1802 |
1703 $message .= '<p>' . sprintf( |
1803 $message .= '<p>' . sprintf( |
1704 /* translators: 1: wp-config.php, 2: Database host. */ |
1804 /* translators: 1: wp-config.php, 2: Database host. */ |
1705 __( '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.' ), |
1805 __( 'This either means that the username and password information in your %1$s file is incorrect or that contact with the database server at %2$s could not be established. This could mean your host’s database server is down.' ), |
1706 '<code>wp-config.php</code>', |
1806 '<code>wp-config.php</code>', |
1707 '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
1807 '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
1708 ) . "</p>\n"; |
1808 ) . "</p>\n"; |
1709 |
1809 |
1710 $message .= "<ul>\n"; |
1810 $message .= "<ul>\n"; |
1713 $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
1813 $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
1714 $message .= "</ul>\n"; |
1814 $message .= "</ul>\n"; |
1715 |
1815 |
1716 $message .= '<p>' . sprintf( |
1816 $message .= '<p>' . sprintf( |
1717 /* translators: %s: Support forums URL. */ |
1817 /* translators: %s: Support forums URL. */ |
1718 __( '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>.' ), |
1818 __( 'If you are 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>.' ), |
1719 __( 'https://wordpress.org/support/forums/' ) |
1819 __( 'https://wordpress.org/support/forums/' ) |
1720 ) . "</p>\n"; |
1820 ) . "</p>\n"; |
1721 |
1821 |
1722 $this->bail( $message, 'db_connect_fail' ); |
1822 $this->bail( $message, 'db_connect_fail' ); |
1723 |
1823 |
1749 * and/or socket file. |
1849 * and/or socket file. |
1750 * |
1850 * |
1751 * @since 4.9.0 |
1851 * @since 4.9.0 |
1752 * |
1852 * |
1753 * @param string $host The DB_HOST setting to parse. |
1853 * @param string $host The DB_HOST setting to parse. |
1754 * @return array|false Array containing the host, the port, the socket and |
1854 * @return array|false { |
1755 * whether it is an IPv6 address, in that order. |
1855 * Array containing the host, the port, the socket and |
1756 * False if $host couldn't be parsed. |
1856 * whether it is an IPv6 address, in that order. |
|
1857 * False if the host couldn't be parsed. |
|
1858 * |
|
1859 * @type string $0 Host name. |
|
1860 * @type string|null $1 Port. |
|
1861 * @type string|null $2 Socket. |
|
1862 * @type bool $3 Whether it is an IPv6 address. |
|
1863 * } |
1757 */ |
1864 */ |
1758 public function parse_db_host( $host ) { |
1865 public function parse_db_host( $host ) { |
1759 $port = null; |
1866 $port = null; |
1760 $socket = null; |
1867 $socket = null; |
1761 $is_ipv6 = false; |
1868 $is_ipv6 = false; |
1799 * Checks that the connection to the database is still up. If not, try to reconnect. |
1906 * Checks that the connection to the database is still up. If not, try to reconnect. |
1800 * |
1907 * |
1801 * If this function is unable to reconnect, it will forcibly die, or if called |
1908 * If this function is unable to reconnect, it will forcibly die, or if called |
1802 * after the {@see 'template_redirect'} hook has been fired, return false instead. |
1909 * after the {@see 'template_redirect'} hook has been fired, return false instead. |
1803 * |
1910 * |
1804 * If $allow_bail is false, the lack of database connection will need to be handled manually. |
1911 * If `$allow_bail` is false, the lack of database connection will need to be handled manually. |
1805 * |
1912 * |
1806 * @since 3.9.0 |
1913 * @since 3.9.0 |
1807 * |
1914 * |
1808 * @param bool $allow_bail Optional. Allows the function to bail. Default true. |
1915 * @param bool $allow_bail Optional. Allows the function to bail. Default true. |
1809 * @return bool|void True if the connection is up. |
1916 * @return bool|void True if the connection is up. |
1859 |
1966 |
1860 $message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n"; |
1967 $message = '<h1>' . __( 'Error reconnecting to the database' ) . "</h1>\n"; |
1861 |
1968 |
1862 $message .= '<p>' . sprintf( |
1969 $message .= '<p>' . sprintf( |
1863 /* translators: %s: Database host. */ |
1970 /* translators: %s: Database host. */ |
1864 __( 'This means that we lost contact with the database server at %s. This could mean your host’s database server is down.' ), |
1971 __( 'This means that the contact with the database server at %s was lost. This could mean your host’s database server is down.' ), |
1865 '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
1972 '<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>' |
1866 ) . "</p>\n"; |
1973 ) . "</p>\n"; |
1867 |
1974 |
1868 $message .= "<ul>\n"; |
1975 $message .= "<ul>\n"; |
1869 $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
1976 $message .= '<li>' . __( 'Are you sure the database server is running?' ) . "</li>\n"; |
1870 $message .= '<li>' . __( 'Are you sure the database server is not under particularly heavy load?' ) . "</li>\n"; |
1977 $message .= '<li>' . __( 'Are you sure the database server is not under particularly heavy load?' ) . "</li>\n"; |
1871 $message .= "</ul>\n"; |
1978 $message .= "</ul>\n"; |
1872 |
1979 |
1873 $message .= '<p>' . sprintf( |
1980 $message .= '<p>' . sprintf( |
1874 /* translators: %s: Support forums URL. */ |
1981 /* translators: %s: Support forums URL. */ |
1875 __( '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>.' ), |
1982 __( 'If you are 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>.' ), |
1876 __( 'https://wordpress.org/support/forums/' ) |
1983 __( 'https://wordpress.org/support/forums/' ) |
1877 ) . "</p>\n"; |
1984 ) . "</p>\n"; |
1878 |
1985 |
1879 // We weren't able to reconnect, so we better bail. |
1986 // We weren't able to reconnect, so we better bail. |
1880 $this->bail( $message, 'db_connect_fail' ); |
1987 $this->bail( $message, 'db_connect_fail' ); |
1930 $stripped_query = $this->strip_invalid_text_from_query( $query ); |
2037 $stripped_query = $this->strip_invalid_text_from_query( $query ); |
1931 // strip_invalid_text_from_query() can perform queries, so we need |
2038 // strip_invalid_text_from_query() can perform queries, so we need |
1932 // to flush again, just to make sure everything is clear. |
2039 // to flush again, just to make sure everything is clear. |
1933 $this->flush(); |
2040 $this->flush(); |
1934 if ( $stripped_query !== $query ) { |
2041 if ( $stripped_query !== $query ) { |
1935 $this->insert_id = 0; |
2042 $this->insert_id = 0; |
|
2043 $this->last_query = $query; |
|
2044 |
|
2045 wp_load_translations_early(); |
|
2046 |
|
2047 $this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' ); |
|
2048 |
1936 return false; |
2049 return false; |
1937 } |
2050 } |
1938 } |
2051 } |
1939 |
2052 |
1940 $this->check_current_query = true; |
2053 $this->check_current_query = true; |
1942 // Keep track of the last query for debug. |
2055 // Keep track of the last query for debug. |
1943 $this->last_query = $query; |
2056 $this->last_query = $query; |
1944 |
2057 |
1945 $this->_do_query( $query ); |
2058 $this->_do_query( $query ); |
1946 |
2059 |
1947 // MySQL server has gone away, try to reconnect. |
2060 // Database server has gone away, try to reconnect. |
1948 $mysql_errno = 0; |
2061 $mysql_errno = 0; |
1949 if ( ! empty( $this->dbh ) ) { |
2062 if ( ! empty( $this->dbh ) ) { |
1950 if ( $this->use_mysqli ) { |
2063 if ( $this->use_mysqli ) { |
1951 if ( $this->dbh instanceof mysqli ) { |
2064 if ( $this->dbh instanceof mysqli ) { |
1952 $mysql_errno = mysqli_errno( $this->dbh ); |
2065 $mysql_errno = mysqli_errno( $this->dbh ); |
2246 * specified in wpdb::$field_types. |
2361 * specified in wpdb::$field_types. |
2247 * @param string $type Optional. Type of operation. Possible values include 'INSERT' or 'REPLACE'. |
2362 * @param string $type Optional. Type of operation. Possible values include 'INSERT' or 'REPLACE'. |
2248 * Default 'INSERT'. |
2363 * Default 'INSERT'. |
2249 * @return int|false The number of rows affected, or false on error. |
2364 * @return int|false The number of rows affected, or false on error. |
2250 */ |
2365 */ |
2251 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { |
2366 public function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { |
2252 $this->insert_id = 0; |
2367 $this->insert_id = 0; |
2253 |
2368 |
2254 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) { |
2369 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) { |
2255 return false; |
2370 return false; |
2256 } |
2371 } |
2283 |
2398 |
2284 /** |
2399 /** |
2285 * Updates a row in the table. |
2400 * Updates a row in the table. |
2286 * |
2401 * |
2287 * Examples: |
2402 * Examples: |
|
2403 * |
2288 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) |
2404 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) |
2289 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
2405 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
2290 * |
2406 * |
2291 * @since 2.5.0 |
2407 * @since 2.5.0 |
2292 * |
2408 * |
2450 } |
2567 } |
2451 |
2568 |
2452 $converted_data = $this->strip_invalid_text( $data ); |
2569 $converted_data = $this->strip_invalid_text( $data ); |
2453 |
2570 |
2454 if ( $data !== $converted_data ) { |
2571 if ( $data !== $converted_data ) { |
|
2572 |
|
2573 $problem_fields = array(); |
|
2574 foreach ( $data as $field => $value ) { |
|
2575 if ( $value !== $converted_data[ $field ] ) { |
|
2576 $problem_fields[] = $field; |
|
2577 } |
|
2578 } |
|
2579 |
|
2580 wp_load_translations_early(); |
|
2581 |
|
2582 if ( 1 === count( $problem_fields ) ) { |
|
2583 $this->last_error = sprintf( |
|
2584 /* translators: %s: Database field where the error occurred. */ |
|
2585 __( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ), |
|
2586 reset( $problem_fields ) |
|
2587 ); |
|
2588 } else { |
|
2589 $this->last_error = sprintf( |
|
2590 /* translators: %s: Database fields where the error occurred. */ |
|
2591 __( 'WordPress database error: Processing the values for the following fields failed: %s. The supplied values may be too long or contain invalid data.' ), |
|
2592 implode( ', ', $problem_fields ) |
|
2593 ); |
|
2594 } |
|
2595 |
2455 return false; |
2596 return false; |
2456 } |
2597 } |
2457 |
2598 |
2458 return $data; |
2599 return $data; |
2459 } |
2600 } |
2571 * @return string|null Database query result (as string), or null on failure. |
2712 * @return string|null Database query result (as string), or null on failure. |
2572 */ |
2713 */ |
2573 public function get_var( $query = null, $x = 0, $y = 0 ) { |
2714 public function get_var( $query = null, $x = 0, $y = 0 ) { |
2574 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; |
2715 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; |
2575 |
2716 |
2576 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2577 $this->check_current_query = false; |
|
2578 } |
|
2579 |
|
2580 if ( $query ) { |
2717 if ( $query ) { |
|
2718 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2719 $this->check_current_query = false; |
|
2720 } |
|
2721 |
2581 $this->query( $query ); |
2722 $this->query( $query ); |
2582 } |
2723 } |
2583 |
2724 |
2584 // Extract var out of cached results based on x,y vals. |
2725 // Extract var out of cached results based on x,y vals. |
2585 if ( ! empty( $this->last_result[ $y ] ) ) { |
2726 if ( ! empty( $this->last_result[ $y ] ) ) { |
2605 * @return array|object|null|void Database query result in format specified by $output or null on failure. |
2746 * @return array|object|null|void Database query result in format specified by $output or null on failure. |
2606 */ |
2747 */ |
2607 public function get_row( $query = null, $output = OBJECT, $y = 0 ) { |
2748 public function get_row( $query = null, $output = OBJECT, $y = 0 ) { |
2608 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
2749 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
2609 |
2750 |
2610 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2611 $this->check_current_query = false; |
|
2612 } |
|
2613 |
|
2614 if ( $query ) { |
2751 if ( $query ) { |
|
2752 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2753 $this->check_current_query = false; |
|
2754 } |
|
2755 |
2615 $this->query( $query ); |
2756 $this->query( $query ); |
2616 } else { |
2757 } else { |
2617 return null; |
2758 return null; |
2618 } |
2759 } |
2619 |
2760 |
2647 * @param string|null $query Optional. SQL query. Defaults to previous query. |
2788 * @param string|null $query Optional. SQL query. Defaults to previous query. |
2648 * @param int $x Optional. Column to return. Indexed from 0. |
2789 * @param int $x Optional. Column to return. Indexed from 0. |
2649 * @return array Database query result. Array indexed from 0 by SQL result row number. |
2790 * @return array Database query result. Array indexed from 0 by SQL result row number. |
2650 */ |
2791 */ |
2651 public function get_col( $query = null, $x = 0 ) { |
2792 public function get_col( $query = null, $x = 0 ) { |
2652 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2653 $this->check_current_query = false; |
|
2654 } |
|
2655 |
|
2656 if ( $query ) { |
2793 if ( $query ) { |
|
2794 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2795 $this->check_current_query = false; |
|
2796 } |
|
2797 |
2657 $this->query( $query ); |
2798 $this->query( $query ); |
2658 } |
2799 } |
2659 |
2800 |
2660 $new_array = array(); |
2801 $new_array = array(); |
2661 // Extract the column values. |
2802 // Extract the column values. |
2685 * @return array|object|null Database query results. |
2826 * @return array|object|null Database query results. |
2686 */ |
2827 */ |
2687 public function get_results( $query = null, $output = OBJECT ) { |
2828 public function get_results( $query = null, $output = OBJECT ) { |
2688 $this->func_call = "\$db->get_results(\"$query\", $output)"; |
2829 $this->func_call = "\$db->get_results(\"$query\", $output)"; |
2689 |
2830 |
2690 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2691 $this->check_current_query = false; |
|
2692 } |
|
2693 |
|
2694 if ( $query ) { |
2831 if ( $query ) { |
|
2832 if ( $this->check_current_query && $this->check_safe_collation( $query ) ) { |
|
2833 $this->check_current_query = false; |
|
2834 } |
|
2835 |
2695 $this->query( $query ); |
2836 $this->query( $query ); |
2696 } else { |
2837 } else { |
2697 return null; |
2838 return null; |
2698 } |
2839 } |
2699 |
2840 |
2747 $tablekey = strtolower( $table ); |
2888 $tablekey = strtolower( $table ); |
2748 |
2889 |
2749 /** |
2890 /** |
2750 * Filters the table charset value before the DB is checked. |
2891 * Filters the table charset value before the DB is checked. |
2751 * |
2892 * |
2752 * Passing a non-null value to the filter will effectively short-circuit |
2893 * Returning a non-null value from the filter will effectively short-circuit |
2753 * checking the DB for the charset, returning that value instead. |
2894 * checking the DB for the charset, returning that value instead. |
2754 * |
2895 * |
2755 * @since 4.2.0 |
2896 * @since 4.2.0 |
2756 * |
2897 * |
2757 * @param string|null $charset The character set to use. Default null. |
2898 * @param string|WP_Error|null $charset The character set to use, WP_Error object |
2758 * @param string $table The name of the table being checked. |
2899 * if it couldn't be found. Default null. |
|
2900 * @param string $table The name of the table being checked. |
2759 */ |
2901 */ |
2760 $charset = apply_filters( 'pre_get_table_charset', null, $table ); |
2902 $charset = apply_filters( 'pre_get_table_charset', null, $table ); |
2761 if ( null !== $charset ) { |
2903 if ( null !== $charset ) { |
2762 return $charset; |
2904 return $charset; |
2763 } |
2905 } |
2771 |
2913 |
2772 $table_parts = explode( '.', $table ); |
2914 $table_parts = explode( '.', $table ); |
2773 $table = '`' . implode( '`.`', $table_parts ) . '`'; |
2915 $table = '`' . implode( '`.`', $table_parts ) . '`'; |
2774 $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" ); |
2916 $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" ); |
2775 if ( ! $results ) { |
2917 if ( ! $results ) { |
2776 return new WP_Error( 'wpdb_get_table_charset_failure' ); |
2918 return new WP_Error( 'wpdb_get_table_charset_failure', __( 'Could not retrieve table charset.' ) ); |
2777 } |
2919 } |
2778 |
2920 |
2779 foreach ( $results as $column ) { |
2921 foreach ( $results as $column ) { |
2780 $columns[ strtolower( $column->Field ) ] = $column; |
2922 $columns[ strtolower( $column->Field ) ] = $column; |
2781 } |
2923 } |
2906 * |
3048 * |
2907 * @since 4.2.1 |
3049 * @since 4.2.1 |
2908 * |
3050 * |
2909 * @param string $table Table name. |
3051 * @param string $table Table name. |
2910 * @param string $column Column name. |
3052 * @param string $column Column name. |
2911 * @return array|false|WP_Error array( 'length' => (int), 'type' => 'byte' | 'char' ). |
3053 * @return array|false|WP_Error { |
2912 * False if the column has no length (for example, numeric column). |
3054 * Array of column length information, false if the column has no length (for |
2913 * WP_Error object if there was an error. |
3055 * example, numeric column), WP_Error object if there was an error. |
|
3056 * |
|
3057 * @type int $length The column length. |
|
3058 * @type string $type One of 'byte' or 'char'. |
2914 */ |
3059 */ |
2915 public function get_col_length( $table, $column ) { |
3060 public function get_col_length( $table, $column ) { |
2916 $tablekey = strtolower( $table ); |
3061 $tablekey = strtolower( $table ); |
2917 $columnkey = strtolower( $column ); |
3062 $columnkey = strtolower( $column ); |
2918 |
3063 |
3213 } |
3358 } |
3214 |
3359 |
3215 $this->check_current_query = false; |
3360 $this->check_current_query = false; |
3216 $row = $this->get_row( 'SELECT ' . implode( ', ', $sql ), ARRAY_A ); |
3361 $row = $this->get_row( 'SELECT ' . implode( ', ', $sql ), ARRAY_A ); |
3217 if ( ! $row ) { |
3362 if ( ! $row ) { |
3218 return new WP_Error( 'wpdb_strip_invalid_text_failure' ); |
3363 return new WP_Error( 'wpdb_strip_invalid_text_failure', __( 'Could not strip invalid text.' ) ); |
3219 } |
3364 } |
3220 |
3365 |
3221 foreach ( array_keys( $data ) as $column ) { |
3366 foreach ( array_keys( $data ) as $column ) { |
3222 if ( isset( $row[ "x_$column" ] ) ) { |
3367 if ( isset( $row[ "x_$column" ] ) ) { |
3223 $data[ $column ]['value'] = $row[ "x_$column" ]; |
3368 $data[ $column ]['value'] = $row[ "x_$column" ]; |
3654 public function get_caller() { |
3798 public function get_caller() { |
3655 return wp_debug_backtrace_summary( __CLASS__ ); |
3799 return wp_debug_backtrace_summary( __CLASS__ ); |
3656 } |
3800 } |
3657 |
3801 |
3658 /** |
3802 /** |
3659 * Retrieves the MySQL server version. |
3803 * Retrieves the database server version. |
3660 * |
3804 * |
3661 * @since 2.7.0 |
3805 * @since 2.7.0 |
3662 * |
3806 * |
3663 * @return string|null Version number on success, null on failure. |
3807 * @return string|null Version number on success, null on failure. |
3664 */ |
3808 */ |
3665 public function db_version() { |
3809 public function db_version() { |
3666 return preg_replace( '/[^0-9.].*/', '', $this->db_server_info() ); |
3810 return preg_replace( '/[^0-9.].*/', '', $this->db_server_info() ); |
3667 } |
3811 } |
3668 |
3812 |
3669 /** |
3813 /** |
3670 * Retrieves full MySQL server information. |
3814 * Retrieves full database server information. |
3671 * |
3815 * |
3672 * @since 5.5.0 |
3816 * @since 5.5.0 |
3673 * |
3817 * |
3674 * @return string|false Server info on success, false on failure. |
3818 * @return string|false Server info on success, false on failure. |
3675 */ |
3819 */ |