135 * @var bool |
169 * @var bool |
136 */ |
170 */ |
137 var $ready = false; |
171 var $ready = false; |
138 |
172 |
139 /** |
173 /** |
|
174 * {@internal Missing Description}} |
|
175 * |
|
176 * @since 3.0.0 |
|
177 * @access public |
|
178 * @var int |
|
179 */ |
|
180 var $blogid = 0; |
|
181 |
|
182 /** |
|
183 * {@internal Missing Description}} |
|
184 * |
|
185 * @since 3.0.0 |
|
186 * @access public |
|
187 * @var int |
|
188 */ |
|
189 var $siteid = 0; |
|
190 |
|
191 /** |
|
192 * List of WordPress per-blog tables |
|
193 * |
|
194 * @since 2.5.0 |
|
195 * @access private |
|
196 * @see wpdb::tables() |
|
197 * @var array |
|
198 */ |
|
199 var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta', |
|
200 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' ); |
|
201 |
|
202 /** |
|
203 * List of deprecated WordPress tables |
|
204 * |
|
205 * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539 |
|
206 * |
|
207 * @since 2.9.0 |
|
208 * @access private |
|
209 * @see wpdb::tables() |
|
210 * @var array |
|
211 */ |
|
212 var $old_tables = array( 'categories', 'post2cat', 'link2cat' ); |
|
213 |
|
214 /** |
|
215 * List of WordPress global tables |
|
216 * |
|
217 * @since 3.0.0 |
|
218 * @access private |
|
219 * @see wpdb::tables() |
|
220 * @var array |
|
221 */ |
|
222 var $global_tables = array( 'users', 'usermeta' ); |
|
223 |
|
224 /** |
|
225 * List of Multisite global tables |
|
226 * |
|
227 * @since 3.0.0 |
|
228 * @access private |
|
229 * @see wpdb::tables() |
|
230 * @var array |
|
231 */ |
|
232 var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta', |
|
233 'sitecategories', 'registration_log', 'blog_versions' ); |
|
234 |
|
235 /** |
|
236 * WordPress Comments table |
|
237 * |
|
238 * @since 1.5.0 |
|
239 * @access public |
|
240 * @var string |
|
241 */ |
|
242 var $comments; |
|
243 |
|
244 /** |
|
245 * WordPress Comment Metadata table |
|
246 * |
|
247 * @since 2.9.0 |
|
248 * @access public |
|
249 * @var string |
|
250 */ |
|
251 var $commentmeta; |
|
252 |
|
253 /** |
|
254 * WordPress Links table |
|
255 * |
|
256 * @since 1.5.0 |
|
257 * @access public |
|
258 * @var string |
|
259 */ |
|
260 var $links; |
|
261 |
|
262 /** |
|
263 * WordPress Options table |
|
264 * |
|
265 * @since 1.5.0 |
|
266 * @access public |
|
267 * @var string |
|
268 */ |
|
269 var $options; |
|
270 |
|
271 /** |
|
272 * WordPress Post Metadata table |
|
273 * |
|
274 * @since 1.5.0 |
|
275 * @access public |
|
276 * @var string |
|
277 */ |
|
278 var $postmeta; |
|
279 |
|
280 /** |
140 * WordPress Posts table |
281 * WordPress Posts table |
141 * |
282 * |
142 * @since 1.5.0 |
283 * @since 1.5.0 |
143 * @access public |
284 * @access public |
144 * @var string |
285 * @var string |
145 */ |
286 */ |
146 var $posts; |
287 var $posts; |
147 |
288 |
148 /** |
289 /** |
|
290 * WordPress Terms table |
|
291 * |
|
292 * @since 2.3.0 |
|
293 * @access public |
|
294 * @var string |
|
295 */ |
|
296 var $terms; |
|
297 |
|
298 /** |
|
299 * WordPress Term Relationships table |
|
300 * |
|
301 * @since 2.3.0 |
|
302 * @access public |
|
303 * @var string |
|
304 */ |
|
305 var $term_relationships; |
|
306 |
|
307 /** |
|
308 * WordPress Term Taxonomy table |
|
309 * |
|
310 * @since 2.3.0 |
|
311 * @access public |
|
312 * @var string |
|
313 */ |
|
314 var $term_taxonomy; |
|
315 |
|
316 /* |
|
317 * Global and Multisite tables |
|
318 */ |
|
319 |
|
320 /** |
|
321 * WordPress User Metadata table |
|
322 * |
|
323 * @since 2.3.0 |
|
324 * @access public |
|
325 * @var string |
|
326 */ |
|
327 var $usermeta; |
|
328 |
|
329 /** |
149 * WordPress Users table |
330 * WordPress Users table |
150 * |
331 * |
151 * @since 1.5.0 |
332 * @since 1.5.0 |
152 * @access public |
333 * @access public |
153 * @var string |
334 * @var string |
154 */ |
335 */ |
155 var $users; |
336 var $users; |
156 |
337 |
157 /** |
338 /** |
158 * WordPress Categories table |
339 * Multisite Blogs table |
159 * |
340 * |
160 * @since 1.5.0 |
341 * @since 3.0.0 |
161 * @access public |
342 * @access public |
162 * @var string |
343 * @var string |
163 */ |
344 */ |
164 var $categories; |
345 var $blogs; |
165 |
346 |
166 /** |
347 /** |
167 * WordPress Post to Category table |
348 * Multisite Blog Versions table |
168 * |
349 * |
169 * @since 1.5.0 |
350 * @since 3.0.0 |
170 * @access public |
351 * @access public |
171 * @var string |
352 * @var string |
172 */ |
353 */ |
173 var $post2cat; |
354 var $blog_versions; |
174 |
355 |
175 /** |
356 /** |
176 * WordPress Comments table |
357 * Multisite Registration Log table |
177 * |
358 * |
178 * @since 1.5.0 |
359 * @since 3.0.0 |
179 * @access public |
360 * @access public |
180 * @var string |
361 * @var string |
181 */ |
362 */ |
182 var $comments; |
363 var $registration_log; |
183 |
364 |
184 /** |
365 /** |
185 * WordPress Links table |
366 * Multisite Signups table |
186 * |
367 * |
187 * @since 1.5.0 |
368 * @since 3.0.0 |
188 * @access public |
369 * @access public |
189 * @var string |
370 * @var string |
190 */ |
371 */ |
191 var $links; |
372 var $signups; |
192 |
373 |
193 /** |
374 /** |
194 * WordPress Options table |
375 * Multisite Sites table |
195 * |
376 * |
196 * @since 1.5.0 |
377 * @since 3.0.0 |
197 * @access public |
378 * @access public |
198 * @var string |
379 * @var string |
199 */ |
380 */ |
200 var $options; |
381 var $site; |
201 |
382 |
202 /** |
383 /** |
203 * WordPress Post Metadata table |
384 * Multisite Sitewide Terms table |
204 * |
385 * |
205 * @since {@internal Version Unknown}} |
386 * @since 3.0.0 |
206 * @access public |
387 * @access public |
207 * @var string |
388 * @var string |
208 */ |
389 */ |
209 var $postmeta; |
390 var $sitecategories; |
210 |
391 |
211 /** |
392 /** |
212 * WordPress Comment Metadata table |
393 * Multisite Site Metadata table |
213 * |
394 * |
214 * @since 2.9 |
395 * @since 3.0.0 |
215 * @access public |
396 * @access public |
216 * @var string |
397 * @var string |
217 */ |
398 */ |
218 var $commentmeta; |
399 var $sitemeta; |
219 |
400 |
220 /** |
401 /** |
221 * WordPress User Metadata table |
402 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load. |
222 * |
403 * |
223 * @since 2.3.0 |
404 * Keys are column names, values are format types: 'ID' => '%d' |
224 * @access public |
405 * |
225 * @var string |
406 * @since 2.8.0 |
226 */ |
407 * @see wpdb::prepare() |
227 var $usermeta; |
408 * @see wpdb::insert() |
228 |
409 * @see wpdb::update() |
229 /** |
410 * @see wpdb::delete() |
230 * WordPress Terms table |
411 * @see wp_set_wpdb_vars() |
231 * |
412 * @access public |
232 * @since 2.3.0 |
|
233 * @access public |
|
234 * @var string |
|
235 */ |
|
236 var $terms; |
|
237 |
|
238 /** |
|
239 * WordPress Term Taxonomy table |
|
240 * |
|
241 * @since 2.3.0 |
|
242 * @access public |
|
243 * @var string |
|
244 */ |
|
245 var $term_taxonomy; |
|
246 |
|
247 /** |
|
248 * WordPress Term Relationships table |
|
249 * |
|
250 * @since 2.3.0 |
|
251 * @access public |
|
252 * @var string |
|
253 */ |
|
254 var $term_relationships; |
|
255 |
|
256 /** |
|
257 * List of WordPress tables |
|
258 * |
|
259 * @since {@internal Version Unknown}} |
|
260 * @access private |
|
261 * @var array |
413 * @var array |
262 */ |
414 */ |
263 var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options', |
415 var $field_types = array(); |
264 'postmeta', 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta'); |
416 |
265 |
417 /** |
266 /** |
418 * Database table columns charset |
267 * List of deprecated WordPress tables |
419 * |
|
420 * @since 2.2.0 |
|
421 * @access public |
|
422 * @var string |
|
423 */ |
|
424 var $charset; |
|
425 |
|
426 /** |
|
427 * Database table columns collate |
|
428 * |
|
429 * @since 2.2.0 |
|
430 * @access public |
|
431 * @var string |
|
432 */ |
|
433 var $collate; |
|
434 |
|
435 /** |
|
436 * Whether to use mysql_real_escape_string |
|
437 * |
|
438 * @since 2.8.0 |
|
439 * @access public |
|
440 * @var bool |
|
441 */ |
|
442 var $real_escape = false; |
|
443 |
|
444 /** |
|
445 * Database Username |
268 * |
446 * |
269 * @since 2.9.0 |
447 * @since 2.9.0 |
270 * @access private |
448 * @access private |
271 * @var array |
449 * @var string |
272 */ |
450 */ |
273 var $old_tables = array('categories', 'post2cat', 'link2cat'); |
451 var $dbuser; |
274 |
452 |
275 |
453 /** |
276 /** |
454 * A textual description of the last query/get_row/get_var call |
277 * Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php. |
455 * |
278 * |
456 * @since 3.0.0 |
279 * Keys are colmn names, values are format types: 'ID' => '%d' |
457 * @access public |
280 * |
458 * @var string |
281 * @since 2.8.0 |
459 */ |
282 * @see wpdb:prepare() |
460 var $func_call; |
283 * @see wpdb:insert() |
461 |
284 * @see wpdb:update() |
462 /** |
285 * @access public |
463 * Whether MySQL is used as the database engine. |
286 * @war array |
464 * |
287 */ |
465 * Set in WPDB::db_connect() to true, by default. This is used when checking |
288 var $field_types = array(); |
466 * against the required MySQL version for WordPress. Normally, a replacement |
289 |
467 * database drop-in (db.php) will skip these checks, but setting this to true |
290 /** |
468 * will force the checks to occur. |
291 * Database table columns charset |
469 * |
292 * |
470 * @since 3.3.0 |
293 * @since 2.2.0 |
|
294 * @access public |
|
295 * @var string |
|
296 */ |
|
297 var $charset; |
|
298 |
|
299 /** |
|
300 * Database table columns collate |
|
301 * |
|
302 * @since 2.2.0 |
|
303 * @access public |
|
304 * @var string |
|
305 */ |
|
306 var $collate; |
|
307 |
|
308 /** |
|
309 * Whether to use mysql_real_escape_string |
|
310 * |
|
311 * @since 2.8.0 |
|
312 * @access public |
471 * @access public |
313 * @var bool |
472 * @var bool |
314 */ |
473 */ |
315 var $real_escape = false; |
474 public $is_mysql = null; |
316 |
|
317 /** |
|
318 * Database Username |
|
319 * |
|
320 * @since 2.9.0 |
|
321 * @access private |
|
322 * @var string |
|
323 */ |
|
324 var $dbuser; |
|
325 |
475 |
326 /** |
476 /** |
327 * Connects to the database server and selects a database |
477 * Connects to the database server and selects a database |
328 * |
478 * |
329 * PHP4 compatibility layer for calling the PHP5 constructor. |
479 * PHP5 style constructor for compatibility with PHP5. Does |
330 * |
480 * the actual setting up of the class properties and connection |
331 * @uses wpdb::__construct() Passes parameters and returns result |
481 * to the database. |
332 * @since 0.71 |
482 * |
|
483 * @link http://core.trac.wordpress.org/ticket/3354 |
|
484 * @since 2.0.8 |
333 * |
485 * |
334 * @param string $dbuser MySQL database user |
486 * @param string $dbuser MySQL database user |
335 * @param string $dbpassword MySQL database password |
487 * @param string $dbpassword MySQL database password |
336 * @param string $dbname MySQL database name |
488 * @param string $dbname MySQL database name |
337 * @param string $dbhost MySQL database host |
489 * @param string $dbhost MySQL database host |
338 */ |
490 */ |
339 function wpdb($dbuser, $dbpassword, $dbname, $dbhost) { |
491 function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
340 return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost); |
492 register_shutdown_function( array( &$this, '__destruct' ) ); |
341 } |
|
342 |
|
343 /** |
|
344 * Connects to the database server and selects a database |
|
345 * |
|
346 * PHP5 style constructor for compatibility with PHP5. Does |
|
347 * the actual setting up of the class properties and connection |
|
348 * to the database. |
|
349 * |
|
350 * @since 2.0.8 |
|
351 * |
|
352 * @param string $dbuser MySQL database user |
|
353 * @param string $dbpassword MySQL database password |
|
354 * @param string $dbname MySQL database name |
|
355 * @param string $dbhost MySQL database host |
|
356 */ |
|
357 function __construct($dbuser, $dbpassword, $dbname, $dbhost) { |
|
358 register_shutdown_function(array(&$this, "__destruct")); |
|
359 |
493 |
360 if ( WP_DEBUG ) |
494 if ( WP_DEBUG ) |
361 $this->show_errors(); |
495 $this->show_errors(); |
362 |
496 |
363 if ( defined('DB_CHARSET') ) |
497 $this->init_charset(); |
|
498 |
|
499 $this->dbuser = $dbuser; |
|
500 $this->dbpassword = $dbpassword; |
|
501 $this->dbname = $dbname; |
|
502 $this->dbhost = $dbhost; |
|
503 |
|
504 $this->db_connect(); |
|
505 } |
|
506 |
|
507 /** |
|
508 * PHP5 style destructor and will run when database object is destroyed. |
|
509 * |
|
510 * @see wpdb::__construct() |
|
511 * @since 2.0.8 |
|
512 * @return bool true |
|
513 */ |
|
514 function __destruct() { |
|
515 return true; |
|
516 } |
|
517 |
|
518 /** |
|
519 * Set $this->charset and $this->collate |
|
520 * |
|
521 * @since 3.1.0 |
|
522 */ |
|
523 function init_charset() { |
|
524 if ( function_exists('is_multisite') && is_multisite() ) { |
|
525 $this->charset = 'utf8'; |
|
526 if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) |
|
527 $this->collate = DB_COLLATE; |
|
528 else |
|
529 $this->collate = 'utf8_general_ci'; |
|
530 } elseif ( defined( 'DB_COLLATE' ) ) { |
|
531 $this->collate = DB_COLLATE; |
|
532 } |
|
533 |
|
534 if ( defined( 'DB_CHARSET' ) ) |
364 $this->charset = DB_CHARSET; |
535 $this->charset = DB_CHARSET; |
365 |
536 } |
366 if ( defined('DB_COLLATE') ) |
537 |
367 $this->collate = DB_COLLATE; |
538 /** |
368 |
539 * Sets the connection's character set. |
369 $this->dbuser = $dbuser; |
540 * |
370 |
541 * @since 3.1.0 |
371 $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true); |
542 * |
372 if (!$this->dbh) { |
543 * @param resource $dbh The resource given by mysql_connect |
373 $this->bail(sprintf(/*WP_I18N_DB_CONN_ERROR*/' |
544 * @param string $charset The character set (optional) |
374 <h1>Erreur lors de l’établissement de la connexion à la base de données</h1> |
545 * @param string $collate The collation (optional) |
375 <p>Cela signifie soit que l’identifiant et/ou le mot de passe indiqués dans votre fichier <code>wp-config.php</code> sont incorrects, ou que le serveur de base de données à l’adresse <code>%s</code> est inaccessible - ce qui implique que le serveur de base de données de votre hébergeur est sans doute défaillant.</p> |
546 */ |
376 <ul> |
547 function set_charset($dbh, $charset = null, $collate = null) { |
377 <li>Êtes-vous certain(e) d’avoir correctement indiqué votre identifiant et votre mot de passe ?</li> |
548 if ( !isset($charset) ) |
378 <li>Êtes-vous certain(e) d’avoir entré le bon serveur de base de données ?</li> |
549 $charset = $this->charset; |
379 <li>Êtes-vous certain(e) que le serveur de base de données fonctionne correctement ?</li> |
550 if ( !isset($collate) ) |
380 </ul> |
551 $collate = $this->collate; |
381 <p>Si vous n’êtes pas sûr(e) de bien comprendre les mots de cette liste, vous devriez sans doute prendre contact avec votre hébergeur. Si malgré cela cette erreur s’affiche toujours, indiquez votre problème au <a href=\'http://www.wordpress-fr.net/support/\'>forum d’entraide en français</a>.</p> |
552 if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) { |
382 '/*/WP_I18N_DB_CONN_ERROR*/, $dbhost), 'db_connect_fail'); |
553 if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) { |
383 return; |
554 mysql_set_charset( $charset, $dbh ); |
384 } |
|
385 |
|
386 $this->ready = true; |
|
387 |
|
388 if ( $this->has_cap( 'collation' ) && !empty($this->charset) ) { |
|
389 if ( function_exists('mysql_set_charset') ) { |
|
390 mysql_set_charset($this->charset, $this->dbh); |
|
391 $this->real_escape = true; |
555 $this->real_escape = true; |
392 } else { |
556 } else { |
393 $collation_query = "SET NAMES '{$this->charset}'"; |
557 $query = $this->prepare( 'SET NAMES %s', $charset ); |
394 if ( !empty($this->collate) ) |
558 if ( ! empty( $collate ) ) |
395 $collation_query .= " COLLATE '{$this->collate}'"; |
559 $query .= $this->prepare( ' COLLATE %s', $collate ); |
396 $this->query($collation_query); |
560 mysql_query( $query, $dbh ); |
397 } |
561 } |
398 } |
562 } |
399 |
|
400 $this->select($dbname); |
|
401 } |
|
402 |
|
403 /** |
|
404 * PHP5 style destructor and will run when database object is destroyed. |
|
405 * |
|
406 * @since 2.0.8 |
|
407 * |
|
408 * @return bool Always true |
|
409 */ |
|
410 function __destruct() { |
|
411 return true; |
|
412 } |
563 } |
413 |
564 |
414 /** |
565 /** |
415 * Sets the table prefix for the WordPress tables. |
566 * Sets the table prefix for the WordPress tables. |
416 * |
567 * |
|
568 * @since 2.5.0 |
|
569 * |
|
570 * @param string $prefix Alphanumeric name for the new prefix. |
|
571 * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not. |
|
572 * @return string|WP_Error Old prefix or WP_Error on error |
|
573 */ |
|
574 function set_prefix( $prefix, $set_table_names = true ) { |
|
575 |
|
576 if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) |
|
577 return new WP_Error('invalid_db_prefix', 'Invalid database prefix' ); |
|
578 |
|
579 $old_prefix = is_multisite() ? '' : $prefix; |
|
580 |
|
581 if ( isset( $this->base_prefix ) ) |
|
582 $old_prefix = $this->base_prefix; |
|
583 |
|
584 $this->base_prefix = $prefix; |
|
585 |
|
586 if ( $set_table_names ) { |
|
587 foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) |
|
588 $this->$table = $prefixed_table; |
|
589 |
|
590 if ( is_multisite() && empty( $this->blogid ) ) |
|
591 return $old_prefix; |
|
592 |
|
593 $this->prefix = $this->get_blog_prefix(); |
|
594 |
|
595 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) |
|
596 $this->$table = $prefixed_table; |
|
597 |
|
598 foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) |
|
599 $this->$table = $prefixed_table; |
|
600 } |
|
601 return $old_prefix; |
|
602 } |
|
603 |
|
604 /** |
|
605 * Sets blog id. |
|
606 * |
|
607 * @since 3.0.0 |
|
608 * @access public |
|
609 * @param int $blog_id |
|
610 * @param int $site_id Optional. |
|
611 * @return string previous blog id |
|
612 */ |
|
613 function set_blog_id( $blog_id, $site_id = 0 ) { |
|
614 if ( ! empty( $site_id ) ) |
|
615 $this->siteid = $site_id; |
|
616 |
|
617 $old_blog_id = $this->blogid; |
|
618 $this->blogid = $blog_id; |
|
619 |
|
620 $this->prefix = $this->get_blog_prefix(); |
|
621 |
|
622 foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) |
|
623 $this->$table = $prefixed_table; |
|
624 |
|
625 foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) |
|
626 $this->$table = $prefixed_table; |
|
627 |
|
628 return $old_blog_id; |
|
629 } |
|
630 |
|
631 /** |
|
632 * Gets blog prefix. |
|
633 * |
|
634 * @uses is_multisite() |
|
635 * @since 3.0.0 |
|
636 * @param int $blog_id Optional. |
|
637 * @return string Blog prefix. |
|
638 */ |
|
639 function get_blog_prefix( $blog_id = null ) { |
|
640 if ( is_multisite() ) { |
|
641 if ( null === $blog_id ) |
|
642 $blog_id = $this->blogid; |
|
643 $blog_id = (int) $blog_id; |
|
644 if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) |
|
645 return $this->base_prefix; |
|
646 else |
|
647 return $this->base_prefix . $blog_id . '_'; |
|
648 } else { |
|
649 return $this->base_prefix; |
|
650 } |
|
651 } |
|
652 |
|
653 /** |
|
654 * Returns an array of WordPress tables. |
|
655 * |
417 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to |
656 * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to |
418 * override the WordPress users and usersmeta tables that would otherwise be determined by the $prefix. |
657 * override the WordPress users and usermeta tables that would otherwise |
419 * |
658 * be determined by the prefix. |
420 * @since 2.5.0 |
659 * |
421 * |
660 * The scope argument can take one of the following: |
422 * @param string $prefix Alphanumeric name for the new prefix. |
661 * |
423 * @return string|WP_Error Old prefix or WP_Error on error |
662 * 'all' - returns 'all' and 'global' tables. No old tables are returned. |
424 */ |
663 * 'blog' - returns the blog-level tables for the queried blog. |
425 function set_prefix($prefix) { |
664 * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite. |
426 |
665 * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. |
427 if ( preg_match('|[^a-z0-9_]|i', $prefix) ) |
666 * 'old' - returns tables which are deprecated. |
428 return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Préfixe de la base de données invalide'/*/WP_I18N_DB_BAD_PREFIX*/); |
667 * |
429 |
668 * @since 3.0.0 |
430 $old_prefix = $this->prefix; |
669 * @uses wpdb::$tables |
431 $this->prefix = $prefix; |
670 * @uses wpdb::$old_tables |
432 |
671 * @uses wpdb::$global_tables |
433 foreach ( (array) $this->tables as $table ) |
672 * @uses wpdb::$ms_global_tables |
434 $this->$table = $this->prefix . $table; |
673 * @uses is_multisite() |
435 |
674 * |
436 if ( defined('CUSTOM_USER_TABLE') ) |
675 * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all. |
437 $this->users = CUSTOM_USER_TABLE; |
676 * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog |
438 |
677 * prefix is requested, then the custom users and usermeta tables will be mapped. |
439 if ( defined('CUSTOM_USER_META_TABLE') ) |
678 * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested. |
440 $this->usermeta = CUSTOM_USER_META_TABLE; |
679 * @return array Table names. When a prefix is requested, the key is the unprefixed table name. |
441 |
680 */ |
442 return $old_prefix; |
681 function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { |
|
682 switch ( $scope ) { |
|
683 case 'all' : |
|
684 $tables = array_merge( $this->global_tables, $this->tables ); |
|
685 if ( is_multisite() ) |
|
686 $tables = array_merge( $tables, $this->ms_global_tables ); |
|
687 break; |
|
688 case 'blog' : |
|
689 $tables = $this->tables; |
|
690 break; |
|
691 case 'global' : |
|
692 $tables = $this->global_tables; |
|
693 if ( is_multisite() ) |
|
694 $tables = array_merge( $tables, $this->ms_global_tables ); |
|
695 break; |
|
696 case 'ms_global' : |
|
697 $tables = $this->ms_global_tables; |
|
698 break; |
|
699 case 'old' : |
|
700 $tables = $this->old_tables; |
|
701 break; |
|
702 default : |
|
703 return array(); |
|
704 break; |
|
705 } |
|
706 |
|
707 if ( $prefix ) { |
|
708 if ( ! $blog_id ) |
|
709 $blog_id = $this->blogid; |
|
710 $blog_prefix = $this->get_blog_prefix( $blog_id ); |
|
711 $base_prefix = $this->base_prefix; |
|
712 $global_tables = array_merge( $this->global_tables, $this->ms_global_tables ); |
|
713 foreach ( $tables as $k => $table ) { |
|
714 if ( in_array( $table, $global_tables ) ) |
|
715 $tables[ $table ] = $base_prefix . $table; |
|
716 else |
|
717 $tables[ $table ] = $blog_prefix . $table; |
|
718 unset( $tables[ $k ] ); |
|
719 } |
|
720 |
|
721 if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) |
|
722 $tables['users'] = CUSTOM_USER_TABLE; |
|
723 |
|
724 if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) |
|
725 $tables['usermeta'] = CUSTOM_USER_META_TABLE; |
|
726 } |
|
727 |
|
728 return $tables; |
443 } |
729 } |
444 |
730 |
445 /** |
731 /** |
446 * Selects a database using the current database connection. |
732 * Selects a database using the current database connection. |
447 * |
733 * |
519 } |
842 } |
520 |
843 |
521 /** |
844 /** |
522 * Escapes content by reference for insertion into the database, for security |
845 * Escapes content by reference for insertion into the database, for security |
523 * |
846 * |
|
847 * @uses wpdb::_real_escape() |
524 * @since 2.3.0 |
848 * @since 2.3.0 |
525 * |
849 * @param string $string to escape |
526 * @param string $s |
850 * @return void |
527 */ |
851 */ |
528 function escape_by_ref(&$string) { |
852 function escape_by_ref( &$string ) { |
529 $string = $this->_real_escape( $string ); |
853 $string = $this->_real_escape( $string ); |
530 } |
854 } |
531 |
855 |
532 /** |
856 /** |
533 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. |
857 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. |
534 * |
858 * |
535 * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string). |
859 * The following directives can be used in the query format string: |
|
860 * %d (integer) |
|
861 * %f (float) |
|
862 * %s (string) |
|
863 * %% (literal percentage sign - no argument needed) |
|
864 * |
|
865 * All of %d, %f, and %s are to be left unquoted in the query string and they need an argument passed for them. |
|
866 * Literals (%) as parts of the query must be properly written as %%. |
|
867 * |
|
868 * This function only supports a small subset of the sprintf syntax; it only supports %d (integer), %f (float), and %s (string). |
536 * Does not support sign, padding, alignment, width or precision specifiers. |
869 * Does not support sign, padding, alignment, width or precision specifiers. |
537 * Does not support argument numbering/swapping. |
870 * Does not support argument numbering/swapping. |
538 * |
871 * |
539 * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. |
872 * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. |
540 * |
873 * |
541 * Both %d and %s should be left unquoted in the query string. |
874 * Both %d and %s should be left unquoted in the query string. |
542 * |
875 * |
543 * <code> |
876 * <code> |
544 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 ) |
877 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 ) |
|
878 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); |
545 * </code> |
879 * </code> |
546 * |
880 * |
547 * @link http://php.net/sprintf Description of syntax. |
881 * @link http://php.net/sprintf Description of syntax. |
548 * @since 2.3.0 |
882 * @since 2.3.0 |
549 * |
883 * |
550 * @param string $query Query statement with sprintf()-like placeholders |
884 * @param string $query Query statement with sprintf()-like placeholders |
551 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. |
885 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like |
552 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}. |
886 * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if |
553 * @return null|string Sanitized query string |
887 * being called like {@link http://php.net/sprintf sprintf()}. |
554 */ |
888 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like |
555 function prepare($query = null) { // ( $query, *$args ) |
889 * {@link http://php.net/sprintf sprintf()}. |
|
890 * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string |
|
891 * if there was something to prepare |
|
892 */ |
|
893 function prepare( $query = null ) { // ( $query, *$args ) |
556 if ( is_null( $query ) ) |
894 if ( is_null( $query ) ) |
557 return; |
895 return; |
|
896 |
558 $args = func_get_args(); |
897 $args = func_get_args(); |
559 array_shift($args); |
898 array_shift( $args ); |
560 // If args were passed as an array (as in vsprintf), move them up |
899 // If args were passed as an array (as in vsprintf), move them up |
561 if ( isset($args[0]) && is_array($args[0]) ) |
900 if ( isset( $args[0] ) && is_array($args[0]) ) |
562 $args = $args[0]; |
901 $args = $args[0]; |
563 $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it |
902 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it |
564 $query = str_replace('"%s"', '%s', $query); // doublequote unquoting |
903 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting |
565 $query = str_replace('%s', "'%s'", $query); // quote the strings |
904 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s |
566 array_walk($args, array(&$this, 'escape_by_ref')); |
905 array_walk( $args, array( &$this, 'escape_by_ref' ) ); |
567 return @vsprintf($query, $args); |
906 return @vsprintf( $query, $args ); |
568 } |
907 } |
569 |
908 |
570 /** |
909 /** |
571 * Print SQL/DB error. |
910 * Print SQL/DB error. |
572 * |
911 * |
635 } |
981 } |
636 |
982 |
637 /** |
983 /** |
638 * Disables showing of database errors. |
984 * Disables showing of database errors. |
639 * |
985 * |
|
986 * By default database errors are not shown. |
|
987 * |
640 * @since 0.71 |
988 * @since 0.71 |
641 * |
989 * @see wpdb::show_errors() |
642 * @return bool Whether showing of errors was active or not |
990 * |
|
991 * @return bool Whether showing of errors was active |
643 */ |
992 */ |
644 function hide_errors() { |
993 function hide_errors() { |
645 $show = $this->show_errors; |
994 $show = $this->show_errors; |
646 $this->show_errors = false; |
995 $this->show_errors = false; |
647 return $show; |
996 return $show; |
648 } |
997 } |
649 |
998 |
650 /** |
999 /** |
651 * Whether to suppress database errors. |
1000 * Whether to suppress database errors. |
652 * |
1001 * |
653 * @param unknown_type $suppress |
1002 * By default database errors are suppressed, with a simple |
654 * @return unknown |
1003 * call to this function they can be enabled. |
|
1004 * |
|
1005 * @since 2.5.0 |
|
1006 * @see wpdb::hide_errors() |
|
1007 * @param bool $suppress Optional. New value. Defaults to true. |
|
1008 * @return bool Old value |
655 */ |
1009 */ |
656 function suppress_errors( $suppress = true ) { |
1010 function suppress_errors( $suppress = true ) { |
657 $errors = $this->suppress_errors; |
1011 $errors = $this->suppress_errors; |
658 $this->suppress_errors = $suppress; |
1012 $this->suppress_errors = (bool) $suppress; |
659 return $errors; |
1013 return $errors; |
660 } |
1014 } |
661 |
1015 |
662 /** |
1016 /** |
663 * Kill cached query results. |
1017 * Kill cached query results. |
664 * |
1018 * |
665 * @since 0.71 |
1019 * @since 0.71 |
|
1020 * @return void |
666 */ |
1021 */ |
667 function flush() { |
1022 function flush() { |
668 $this->last_result = array(); |
1023 $this->last_result = array(); |
669 $this->col_info = null; |
1024 $this->col_info = null; |
670 $this->last_query = null; |
1025 $this->last_query = null; |
|
1026 } |
|
1027 |
|
1028 /** |
|
1029 * Connect to and select database |
|
1030 * |
|
1031 * @since 3.0.0 |
|
1032 */ |
|
1033 function db_connect() { |
|
1034 |
|
1035 $this->is_mysql = true; |
|
1036 |
|
1037 if ( WP_DEBUG ) { |
|
1038 $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); |
|
1039 } else { |
|
1040 $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true ); |
|
1041 } |
|
1042 |
|
1043 if ( !$this->dbh ) { |
|
1044 wp_load_translations_early(); |
|
1045 $this->bail( sprintf( __( " |
|
1046 <h1>Error establishing a database connection</h1> |
|
1047 <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p> |
|
1048 <ul> |
|
1049 <li>Are you sure you have the correct username and password?</li> |
|
1050 <li>Are you sure that you have typed the correct hostname?</li> |
|
1051 <li>Are you sure that the database server is running?</li> |
|
1052 </ul> |
|
1053 <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p> |
|
1054 " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); |
|
1055 |
|
1056 return; |
|
1057 } |
|
1058 |
|
1059 $this->set_charset( $this->dbh ); |
|
1060 |
|
1061 $this->ready = true; |
|
1062 |
|
1063 $this->select( $this->dbname, $this->dbh ); |
671 } |
1064 } |
672 |
1065 |
673 /** |
1066 /** |
674 * Perform a MySQL database query, using current database connection. |
1067 * Perform a MySQL database query, using current database connection. |
675 * |
1068 * |
676 * More information can be found on the codex page. |
1069 * More information can be found on the codex page. |
677 * |
1070 * |
678 * @since 0.71 |
1071 * @since 0.71 |
679 * |
1072 * |
680 * @param string $query |
1073 * @param string $query Database query |
681 * @return int|false Number of rows affected/selected or false on error |
1074 * @return int|false Number of rows affected/selected or false on error |
682 */ |
1075 */ |
683 function query($query) { |
1076 function query( $query ) { |
684 if ( ! $this->ready ) |
1077 if ( ! $this->ready ) |
685 return false; |
1078 return false; |
686 |
1079 |
687 // filter the query, if filters are available |
1080 // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method |
688 // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method |
1081 $query = apply_filters( 'query', $query ); |
689 if ( function_exists('apply_filters') ) |
1082 |
690 $query = apply_filters('query', $query); |
|
691 |
|
692 // initialise return |
|
693 $return_val = 0; |
1083 $return_val = 0; |
694 $this->flush(); |
1084 $this->flush(); |
695 |
1085 |
696 // Log how the function was called |
1086 // Log how the function was called |
697 $this->func_call = "\$db->query(\"$query\")"; |
1087 $this->func_call = "\$db->query(\"$query\")"; |
698 |
1088 |
699 // Keep track of the last query for debug.. |
1089 // Keep track of the last query for debug.. |
700 $this->last_query = $query; |
1090 $this->last_query = $query; |
701 |
1091 |
702 // Perform the query via std mysql_query function.. |
1092 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) |
703 if ( defined('SAVEQUERIES') && SAVEQUERIES ) |
|
704 $this->timer_start(); |
1093 $this->timer_start(); |
705 |
1094 |
706 $this->result = @mysql_query($query, $this->dbh); |
1095 $this->result = @mysql_query( $query, $this->dbh ); |
707 ++$this->num_queries; |
1096 $this->num_queries++; |
708 |
1097 |
709 if ( defined('SAVEQUERIES') && SAVEQUERIES ) |
1098 if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) |
710 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); |
1099 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); |
711 |
1100 |
712 // If there is an error then take note of it.. |
1101 // If there is an error then take note of it.. |
713 if ( $this->last_error = mysql_error($this->dbh) ) { |
1102 if ( $this->last_error = mysql_error( $this->dbh ) ) { |
714 $this->print_error(); |
1103 $this->print_error(); |
715 return false; |
1104 return false; |
716 } |
1105 } |
717 |
1106 |
718 if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) { |
1107 if ( preg_match( '/^\s*(create|alter|truncate|drop) /i', $query ) ) { |
719 $this->rows_affected = mysql_affected_rows($this->dbh); |
1108 $return_val = $this->result; |
|
1109 } elseif ( preg_match( '/^\s*(insert|delete|update|replace) /i', $query ) ) { |
|
1110 $this->rows_affected = mysql_affected_rows( $this->dbh ); |
720 // Take note of the insert_id |
1111 // Take note of the insert_id |
721 if ( preg_match("/^\\s*(insert|replace) /i",$query) ) { |
1112 if ( preg_match( '/^\s*(insert|replace) /i', $query ) ) { |
722 $this->insert_id = mysql_insert_id($this->dbh); |
1113 $this->insert_id = mysql_insert_id($this->dbh); |
723 } |
1114 } |
724 // Return number of rows affected |
1115 // Return number of rows affected |
725 $return_val = $this->rows_affected; |
1116 $return_val = $this->rows_affected; |
726 } else { |
1117 } else { |
727 $i = 0; |
1118 $i = 0; |
728 while ($i < @mysql_num_fields($this->result)) { |
1119 while ( $i < @mysql_num_fields( $this->result ) ) { |
729 $this->col_info[$i] = @mysql_fetch_field($this->result); |
1120 $this->col_info[$i] = @mysql_fetch_field( $this->result ); |
730 $i++; |
1121 $i++; |
731 } |
1122 } |
732 $num_rows = 0; |
1123 $num_rows = 0; |
733 while ( $row = @mysql_fetch_object($this->result) ) { |
1124 while ( $row = @mysql_fetch_object( $this->result ) ) { |
734 $this->last_result[$num_rows] = $row; |
1125 $this->last_result[$num_rows] = $row; |
735 $num_rows++; |
1126 $num_rows++; |
736 } |
1127 } |
737 |
1128 |
738 @mysql_free_result($this->result); |
1129 @mysql_free_result( $this->result ); |
739 |
1130 |
740 // Log number of rows the query returned |
1131 // Log number of rows the query returned |
|
1132 // and return number of rows selected |
741 $this->num_rows = $num_rows; |
1133 $this->num_rows = $num_rows; |
742 |
1134 $return_val = $num_rows; |
743 // Return number of rows selected |
|
744 $return_val = $this->num_rows; |
|
745 } |
1135 } |
746 |
1136 |
747 return $return_val; |
1137 return $return_val; |
748 } |
1138 } |
749 |
1139 |
750 /** |
1140 /** |
751 * Insert a row into a table. |
1141 * Insert a row into a table. |
752 * |
1142 * |
753 * <code> |
1143 * <code> |
|
1144 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
754 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
1145 * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
755 * </code> |
1146 * </code> |
756 * |
1147 * |
757 * @since 2.5.0 |
1148 * @since 2.5.0 |
758 * @see wpdb::prepare() |
1149 * @see wpdb::prepare() |
|
1150 * @see wpdb::$field_types |
|
1151 * @see wp_set_wpdb_vars() |
759 * |
1152 * |
760 * @param string $table table name |
1153 * @param string $table table name |
761 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
1154 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
762 * @param array|string $format (optional) An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings. |
1155 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. |
|
1156 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
763 * @return int|false The number of rows inserted, or false on error. |
1157 * @return int|false The number of rows inserted, or false on error. |
764 */ |
1158 */ |
765 function insert($table, $data, $format = null) { |
1159 function insert( $table, $data, $format = null ) { |
|
1160 return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' ); |
|
1161 } |
|
1162 |
|
1163 /** |
|
1164 * Replace a row into a table. |
|
1165 * |
|
1166 * <code> |
|
1167 * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
|
1168 * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
|
1169 * </code> |
|
1170 * |
|
1171 * @since 3.0.0 |
|
1172 * @see wpdb::prepare() |
|
1173 * @see wpdb::$field_types |
|
1174 * @see wp_set_wpdb_vars() |
|
1175 * |
|
1176 * @param string $table table name |
|
1177 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
|
1178 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. |
|
1179 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|
1180 * @return int|false The number of rows affected, or false on error. |
|
1181 */ |
|
1182 function replace( $table, $data, $format = null ) { |
|
1183 return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' ); |
|
1184 } |
|
1185 |
|
1186 /** |
|
1187 * Helper function for insert and replace. |
|
1188 * |
|
1189 * Runs an insert or replace query based on $type argument. |
|
1190 * |
|
1191 * @access private |
|
1192 * @since 3.0.0 |
|
1193 * @see wpdb::prepare() |
|
1194 * @see wpdb::$field_types |
|
1195 * @see wp_set_wpdb_vars() |
|
1196 * |
|
1197 * @param string $table table name |
|
1198 * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
|
1199 * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. |
|
1200 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|
1201 * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. |
|
1202 * @return int|false The number of rows affected, or false on error. |
|
1203 */ |
|
1204 function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { |
|
1205 if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) |
|
1206 return false; |
766 $formats = $format = (array) $format; |
1207 $formats = $format = (array) $format; |
767 $fields = array_keys($data); |
1208 $fields = array_keys( $data ); |
768 $formatted_fields = array(); |
1209 $formatted_fields = array(); |
769 foreach ( $fields as $field ) { |
1210 foreach ( $fields as $field ) { |
770 if ( !empty($format) ) |
1211 if ( !empty( $format ) ) |
771 $form = ( $form = array_shift($formats) ) ? $form : $format[0]; |
1212 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; |
772 elseif ( isset($this->field_types[$field]) ) |
1213 elseif ( isset( $this->field_types[$field] ) ) |
773 $form = $this->field_types[$field]; |
1214 $form = $this->field_types[$field]; |
774 else |
1215 else |
775 $form = '%s'; |
1216 $form = '%s'; |
776 $formatted_fields[] = $form; |
1217 $formatted_fields[] = $form; |
777 } |
1218 } |
778 $sql = "INSERT INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES ('" . implode( "','", $formatted_fields ) . "')"; |
1219 $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")"; |
779 return $this->query( $this->prepare( $sql, $data) ); |
1220 return $this->query( $this->prepare( $sql, $data ) ); |
780 } |
1221 } |
781 |
|
782 |
1222 |
783 /** |
1223 /** |
784 * Update a row in the table |
1224 * Update a row in the table |
785 * |
1225 * |
786 * <code> |
1226 * <code> |
|
1227 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) |
787 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
1228 * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
788 * </code> |
1229 * </code> |
789 * |
1230 * |
790 * @since 2.5.0 |
1231 * @since 2.5.0 |
791 * @see wpdb::prepare() |
1232 * @see wpdb::prepare() |
|
1233 * @see wpdb::$field_types |
|
1234 * @see wp_set_wpdb_vars() |
792 * |
1235 * |
793 * @param string $table table name |
1236 * @param string $table table name |
794 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
1237 * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
795 * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". |
1238 * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". |
796 * @param array|string $format (optional) An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $data will be treated as strings. |
1239 * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. |
797 * @param array|string $format_where (optional) An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%s' (decimal number, string). If omitted, all values in $where will be treated as strings. |
1240 * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|
1241 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings. |
798 * @return int|false The number of rows updated, or false on error. |
1242 * @return int|false The number of rows updated, or false on error. |
799 */ |
1243 */ |
800 function update($table, $data, $where, $format = null, $where_format = null) { |
1244 function update( $table, $data, $where, $format = null, $where_format = null ) { |
801 if ( !is_array( $where ) ) |
1245 if ( ! is_array( $data ) || ! is_array( $where ) ) |
802 return false; |
1246 return false; |
803 |
1247 |
804 $formats = $format = (array) $format; |
1248 $formats = $format = (array) $format; |
805 $bits = $wheres = array(); |
1249 $bits = $wheres = array(); |
806 foreach ( (array) array_keys($data) as $field ) { |
1250 foreach ( (array) array_keys( $data ) as $field ) { |
807 if ( !empty($format) ) |
1251 if ( !empty( $format ) ) |
808 $form = ( $form = array_shift($formats) ) ? $form : $format[0]; |
1252 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; |
809 elseif ( isset($this->field_types[$field]) ) |
1253 elseif ( isset($this->field_types[$field]) ) |
810 $form = $this->field_types[$field]; |
1254 $form = $this->field_types[$field]; |
811 else |
1255 else |
812 $form = '%s'; |
1256 $form = '%s'; |
813 $bits[] = "`$field` = {$form}"; |
1257 $bits[] = "`$field` = {$form}"; |
814 } |
1258 } |
815 |
1259 |
816 $where_formats = $where_format = (array) $where_format; |
1260 $where_formats = $where_format = (array) $where_format; |
817 foreach ( (array) array_keys($where) as $field ) { |
1261 foreach ( (array) array_keys( $where ) as $field ) { |
818 if ( !empty($where_format) ) |
1262 if ( !empty( $where_format ) ) |
819 $form = ( $form = array_shift($where_formats) ) ? $form : $where_format[0]; |
1263 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; |
820 elseif ( isset($this->field_types[$field]) ) |
1264 elseif ( isset( $this->field_types[$field] ) ) |
821 $form = $this->field_types[$field]; |
1265 $form = $this->field_types[$field]; |
822 else |
1266 else |
823 $form = '%s'; |
1267 $form = '%s'; |
824 $wheres[] = "`$field` = {$form}"; |
1268 $wheres[] = "`$field` = {$form}"; |
825 } |
1269 } |
826 |
1270 |
827 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); |
1271 $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); |
828 return $this->query( $this->prepare( $sql, array_merge(array_values($data), array_values($where))) ); |
1272 return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); |
829 } |
1273 } |
|
1274 |
|
1275 /** |
|
1276 * Delete a row in the table |
|
1277 * |
|
1278 * <code> |
|
1279 * wpdb::delete( 'table', array( 'ID' => 1 ) ) |
|
1280 * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) |
|
1281 * </code> |
|
1282 * |
|
1283 * @since 3.4.0 |
|
1284 * @see wpdb::prepare() |
|
1285 * @see wpdb::$field_types |
|
1286 * @see wp_set_wpdb_vars() |
|
1287 * |
|
1288 * @param string $table table name |
|
1289 * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". |
|
1290 * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types. |
|
1291 * @return int|false The number of rows updated, or false on error. |
|
1292 */ |
|
1293 function delete( $table, $where, $where_format = null ) { |
|
1294 if ( ! is_array( $where ) ) |
|
1295 return false; |
|
1296 |
|
1297 $bits = $wheres = array(); |
|
1298 |
|
1299 $where_formats = $where_format = (array) $where_format; |
|
1300 |
|
1301 foreach ( array_keys( $where ) as $field ) { |
|
1302 if ( !empty( $where_format ) ) { |
|
1303 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; |
|
1304 } elseif ( isset( $this->field_types[ $field ] ) ) { |
|
1305 $form = $this->field_types[ $field ]; |
|
1306 } else { |
|
1307 $form = '%s'; |
|
1308 } |
|
1309 |
|
1310 $wheres[] = "$field = $form"; |
|
1311 } |
|
1312 |
|
1313 $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); |
|
1314 return $this->query( $this->prepare( $sql, $where ) ); |
|
1315 } |
|
1316 |
830 |
1317 |
831 /** |
1318 /** |
832 * Retrieve one variable from the database. |
1319 * Retrieve one variable from the database. |
833 * |
1320 * |
834 * Executes a SQL query and returns the value from the SQL result. |
1321 * Executes a SQL query and returns the value from the SQL result. |
835 * 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. |
1322 * 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. |
836 * If $query is null, this function returns the value in the specified column and row from the previous SQL result. |
1323 * If $query is null, this function returns the value in the specified column and row from the previous SQL result. |
837 * |
1324 * |
838 * @since 0.71 |
1325 * @since 0.71 |
839 * |
1326 * |
840 * @param string|null $query SQL query. If null, use the result from the previous query. |
1327 * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query. |
841 * @param int $x (optional) Column of value to return. Indexed from 0. |
1328 * @param int $x Optional. Column of value to return. Indexed from 0. |
842 * @param int $y (optional) Row of value to return. Indexed from 0. |
1329 * @param int $y Optional. Row of value to return. Indexed from 0. |
843 * @return string Database query result |
1330 * @return string|null Database query result (as string), or null on failure |
844 */ |
1331 */ |
845 function get_var($query=null, $x = 0, $y = 0) { |
1332 function get_var( $query = null, $x = 0, $y = 0 ) { |
846 $this->func_call = "\$db->get_var(\"$query\",$x,$y)"; |
1333 $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; |
847 if ( $query ) |
1334 if ( $query ) |
848 $this->query($query); |
1335 $this->query( $query ); |
849 |
1336 |
850 // Extract var out of cached results based x,y vals |
1337 // Extract var out of cached results based x,y vals |
851 if ( !empty( $this->last_result[$y] ) ) { |
1338 if ( !empty( $this->last_result[$y] ) ) { |
852 $values = array_values(get_object_vars($this->last_result[$y])); |
1339 $values = array_values( get_object_vars( $this->last_result[$y] ) ); |
853 } |
1340 } |
854 |
1341 |
855 // If there is a value return it else return null |
1342 // If there is a value return it else return null |
856 return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; |
1343 return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; |
857 } |
1344 } |
858 |
1345 |
859 /** |
1346 /** |
860 * Retrieve one row from the database. |
1347 * Retrieve one row from the database. |
861 * |
1348 * |
862 * Executes a SQL query and returns the row from the SQL result. |
1349 * Executes a SQL query and returns the row from the SQL result. |
863 * |
1350 * |
864 * @since 0.71 |
1351 * @since 0.71 |
865 * |
1352 * |
866 * @param string|null $query SQL query. |
1353 * @param string|null $query SQL query. |
867 * @param string $output (optional) one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. |
1354 * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), |
868 * @param int $y (optional) Row to return. Indexed from 0. |
1355 * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. |
869 * @return mixed Database query result in format specifed by $output |
1356 * @param int $y Optional. Row to return. Indexed from 0. |
870 */ |
1357 * @return mixed Database query result in format specified by $output or null on failure |
871 function get_row($query = null, $output = OBJECT, $y = 0) { |
1358 */ |
|
1359 function get_row( $query = null, $output = OBJECT, $y = 0 ) { |
872 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
1360 $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
873 if ( $query ) |
1361 if ( $query ) |
874 $this->query($query); |
1362 $this->query( $query ); |
875 else |
1363 else |
876 return null; |
1364 return null; |
877 |
1365 |
878 if ( !isset($this->last_result[$y]) ) |
1366 if ( !isset( $this->last_result[$y] ) ) |
879 return null; |
1367 return null; |
880 |
1368 |
881 if ( $output == OBJECT ) { |
1369 if ( $output == OBJECT ) { |
882 return $this->last_result[$y] ? $this->last_result[$y] : null; |
1370 return $this->last_result[$y] ? $this->last_result[$y] : null; |
883 } elseif ( $output == ARRAY_A ) { |
1371 } elseif ( $output == ARRAY_A ) { |
884 return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null; |
1372 return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; |
885 } elseif ( $output == ARRAY_N ) { |
1373 } elseif ( $output == ARRAY_N ) { |
886 return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null; |
1374 return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; |
887 } else { |
1375 } else { |
888 $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/' $db->get_row(string query, output type, int offset) -- Le type de sortie doit être: OBJECT, ARRAY_A, ARRAY_N'/*/WP_I18N_DB_GETROW_ERROR*/); |
1376 $this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" ); |
889 } |
1377 } |
890 } |
1378 } |
891 |
1379 |
892 /** |
1380 /** |
893 * Retrieve one column from the database. |
1381 * Retrieve one column from the database. |
920 * Executes a SQL query and returns the entire SQL result. |
1408 * Executes a SQL query and returns the entire SQL result. |
921 * |
1409 * |
922 * @since 0.71 |
1410 * @since 0.71 |
923 * |
1411 * |
924 * @param string $query SQL query. |
1412 * @param string $query SQL query. |
925 * @param string $output (optional) ane of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded. |
1413 * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. |
|
1414 * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. |
|
1415 * With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded. |
926 * @return mixed Database query results |
1416 * @return mixed Database query results |
927 */ |
1417 */ |
928 function get_results($query = null, $output = OBJECT) { |
1418 function get_results( $query = null, $output = OBJECT ) { |
929 $this->func_call = "\$db->get_results(\"$query\", $output)"; |
1419 $this->func_call = "\$db->get_results(\"$query\", $output)"; |
930 |
1420 |
931 if ( $query ) |
1421 if ( $query ) |
932 $this->query($query); |
1422 $this->query( $query ); |
933 else |
1423 else |
934 return null; |
1424 return null; |
935 |
1425 |
|
1426 $new_array = array(); |
936 if ( $output == OBJECT ) { |
1427 if ( $output == OBJECT ) { |
937 // Return an integer-keyed array of row objects |
1428 // Return an integer-keyed array of row objects |
938 return $this->last_result; |
1429 return $this->last_result; |
939 } elseif ( $output == OBJECT_K ) { |
1430 } elseif ( $output == OBJECT_K ) { |
940 // Return an array of row objects with keys from column 1 |
1431 // Return an array of row objects with keys from column 1 |
941 // (Duplicates are discarded) |
1432 // (Duplicates are discarded) |
942 foreach ( $this->last_result as $row ) { |
1433 foreach ( $this->last_result as $row ) { |
943 $key = array_shift( get_object_vars( $row ) ); |
1434 $var_by_ref = get_object_vars( $row ); |
944 if ( !isset( $new_array[ $key ] ) ) |
1435 $key = array_shift( $var_by_ref ); |
|
1436 if ( ! isset( $new_array[ $key ] ) ) |
945 $new_array[ $key ] = $row; |
1437 $new_array[ $key ] = $row; |
946 } |
1438 } |
947 return $new_array; |
1439 return $new_array; |
948 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { |
1440 } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { |
949 // Return an integer-keyed array of... |
1441 // Return an integer-keyed array of... |
950 if ( $this->last_result ) { |
1442 if ( $this->last_result ) { |
951 $i = 0; |
|
952 foreach( (array) $this->last_result as $row ) { |
1443 foreach( (array) $this->last_result as $row ) { |
953 if ( $output == ARRAY_N ) { |
1444 if ( $output == ARRAY_N ) { |
954 // ...integer-keyed row arrays |
1445 // ...integer-keyed row arrays |
955 $new_array[$i] = array_values( get_object_vars( $row ) ); |
1446 $new_array[] = array_values( get_object_vars( $row ) ); |
956 } else { |
1447 } else { |
957 // ...column name-keyed row arrays |
1448 // ...column name-keyed row arrays |
958 $new_array[$i] = get_object_vars( $row ); |
1449 $new_array[] = get_object_vars( $row ); |
959 } |
1450 } |
960 ++$i; |
|
961 } |
1451 } |
962 return $new_array; |
|
963 } |
1452 } |
964 } |
1453 return $new_array; |
|
1454 } |
|
1455 return null; |
965 } |
1456 } |
966 |
1457 |
967 /** |
1458 /** |
968 * Retrieve column metadata from the last query. |
1459 * Retrieve column metadata from the last query. |
969 * |
1460 * |
970 * @since 0.71 |
1461 * @since 0.71 |
971 * |
1462 * |
972 * @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill |
1463 * @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 |
973 * @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type |
1464 * @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 |
974 * @return mixed Column Results |
1465 * @return mixed Column Results |
975 */ |
1466 */ |
976 function get_col_info($info_type = 'name', $col_offset = -1) { |
1467 function get_col_info( $info_type = 'name', $col_offset = -1 ) { |
977 if ( $this->col_info ) { |
1468 if ( $this->col_info ) { |
978 if ( $col_offset == -1 ) { |
1469 if ( $col_offset == -1 ) { |
979 $i = 0; |
1470 $i = 0; |
|
1471 $new_array = array(); |
980 foreach( (array) $this->col_info as $col ) { |
1472 foreach( (array) $this->col_info as $col ) { |
981 $new_array[$i] = $col->{$info_type}; |
1473 $new_array[$i] = $col->{$info_type}; |
982 $i++; |
1474 $i++; |
983 } |
1475 } |
984 return $new_array; |
1476 return $new_array; |