PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/wp-db.php

https://bitbucket.org/zachisit/zachis.it-m
PHP | 1605 lines | 572 code | 163 blank | 870 comment | 119 complexity | 8b9983438550184586b2923f34671e2f MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress DB Class
  4. *
  5. * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
  6. *
  7. * @package WordPress
  8. * @subpackage Database
  9. * @since 0.71
  10. */
  11. /**
  12. * @since 0.71
  13. */
  14. define( 'EZSQL_VERSION', 'WP1.25' );
  15. /**
  16. * @since 0.71
  17. */
  18. define( 'OBJECT', 'OBJECT', true );
  19. /**
  20. * @since 2.5.0
  21. */
  22. define( 'OBJECT_K', 'OBJECT_K' );
  23. /**
  24. * @since 0.71
  25. */
  26. define( 'ARRAY_A', 'ARRAY_A' );
  27. /**
  28. * @since 0.71
  29. */
  30. define( 'ARRAY_N', 'ARRAY_N' );
  31. /**
  32. * WordPress Database Access Abstraction Object
  33. *
  34. * It is possible to replace this class with your own
  35. * by setting the $wpdb global variable in wp-content/db.php
  36. * file to your class. The wpdb class will still be included,
  37. * so you can extend it or simply use your own.
  38. *
  39. * @link http://codex.wordpress.org/Function_Reference/wpdb_Class
  40. *
  41. * @package WordPress
  42. * @subpackage Database
  43. * @since 0.71
  44. */
  45. class wpdb {
  46. /**
  47. * Whether to show SQL/DB errors
  48. *
  49. * @since 0.71
  50. * @access private
  51. * @var bool
  52. */
  53. var $show_errors = false;
  54. /**
  55. * Whether to suppress errors during the DB bootstrapping.
  56. *
  57. * @access private
  58. * @since 2.5.0
  59. * @var bool
  60. */
  61. var $suppress_errors = false;
  62. /**
  63. * The last error during query.
  64. *
  65. * @since 2.5.0
  66. * @var string
  67. */
  68. var $last_error = '';
  69. /**
  70. * Amount of queries made
  71. *
  72. * @since 1.2.0
  73. * @access private
  74. * @var int
  75. */
  76. var $num_queries = 0;
  77. /**
  78. * Count of rows returned by previous query
  79. *
  80. * @since 1.2.0
  81. * @access private
  82. * @var int
  83. */
  84. var $num_rows = 0;
  85. /**
  86. * Count of affected rows by previous query
  87. *
  88. * @since 0.71
  89. * @access private
  90. * @var int
  91. */
  92. var $rows_affected = 0;
  93. /**
  94. * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
  95. *
  96. * @since 0.71
  97. * @access public
  98. * @var int
  99. */
  100. var $insert_id = 0;
  101. /**
  102. * Saved result of the last query made
  103. *
  104. * @since 1.2.0
  105. * @access private
  106. * @var array
  107. */
  108. var $last_query;
  109. /**
  110. * Results of the last query made
  111. *
  112. * @since 1.0.0
  113. * @access private
  114. * @var array|null
  115. */
  116. var $last_result;
  117. /**
  118. * Saved info on the table column
  119. *
  120. * @since 1.2.0
  121. * @access private
  122. * @var array
  123. */
  124. var $col_info;
  125. /**
  126. * Saved queries that were executed
  127. *
  128. * @since 1.5.0
  129. * @access private
  130. * @var array
  131. */
  132. var $queries;
  133. /**
  134. * WordPress table prefix
  135. *
  136. * You can set this to have multiple WordPress installations
  137. * in a single database. The second reason is for possible
  138. * security precautions.
  139. *
  140. * @since 0.71
  141. * @access private
  142. * @var string
  143. */
  144. var $prefix = '';
  145. /**
  146. * Whether the database queries are ready to start executing.
  147. *
  148. * @since 2.5.0
  149. * @access private
  150. * @var bool
  151. */
  152. var $ready = false;
  153. /**
  154. * {@internal Missing Description}}
  155. *
  156. * @since 3.0.0
  157. * @access public
  158. * @var int
  159. */
  160. var $blogid = 0;
  161. /**
  162. * {@internal Missing Description}}
  163. *
  164. * @since 3.0.0
  165. * @access public
  166. * @var int
  167. */
  168. var $siteid = 0;
  169. /**
  170. * List of WordPress per-blog tables
  171. *
  172. * @since 2.5.0
  173. * @access private
  174. * @see wpdb::tables()
  175. * @var array
  176. */
  177. var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
  178. 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
  179. /**
  180. * List of deprecated WordPress tables
  181. *
  182. * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
  183. *
  184. * @since 2.9.0
  185. * @access private
  186. * @see wpdb::tables()
  187. * @var array
  188. */
  189. var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
  190. /**
  191. * List of WordPress global tables
  192. *
  193. * @since 3.0.0
  194. * @access private
  195. * @see wpdb::tables()
  196. * @var array
  197. */
  198. var $global_tables = array( 'users', 'usermeta' );
  199. /**
  200. * List of Multisite global tables
  201. *
  202. * @since 3.0.0
  203. * @access private
  204. * @see wpdb::tables()
  205. * @var array
  206. */
  207. var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
  208. 'sitecategories', 'registration_log', 'blog_versions' );
  209. /**
  210. * WordPress Comments table
  211. *
  212. * @since 1.5.0
  213. * @access public
  214. * @var string
  215. */
  216. var $comments;
  217. /**
  218. * WordPress Comment Metadata table
  219. *
  220. * @since 2.9.0
  221. * @access public
  222. * @var string
  223. */
  224. var $commentmeta;
  225. /**
  226. * WordPress Links table
  227. *
  228. * @since 1.5.0
  229. * @access public
  230. * @var string
  231. */
  232. var $links;
  233. /**
  234. * WordPress Options table
  235. *
  236. * @since 1.5.0
  237. * @access public
  238. * @var string
  239. */
  240. var $options;
  241. /**
  242. * WordPress Post Metadata table
  243. *
  244. * @since 1.5.0
  245. * @access public
  246. * @var string
  247. */
  248. var $postmeta;
  249. /**
  250. * WordPress Posts table
  251. *
  252. * @since 1.5.0
  253. * @access public
  254. * @var string
  255. */
  256. var $posts;
  257. /**
  258. * WordPress Terms table
  259. *
  260. * @since 2.3.0
  261. * @access public
  262. * @var string
  263. */
  264. var $terms;
  265. /**
  266. * WordPress Term Relationships table
  267. *
  268. * @since 2.3.0
  269. * @access public
  270. * @var string
  271. */
  272. var $term_relationships;
  273. /**
  274. * WordPress Term Taxonomy table
  275. *
  276. * @since 2.3.0
  277. * @access public
  278. * @var string
  279. */
  280. var $term_taxonomy;
  281. /*
  282. * Global and Multisite tables
  283. */
  284. /**
  285. * WordPress User Metadata table
  286. *
  287. * @since 2.3.0
  288. * @access public
  289. * @var string
  290. */
  291. var $usermeta;
  292. /**
  293. * WordPress Users table
  294. *
  295. * @since 1.5.0
  296. * @access public
  297. * @var string
  298. */
  299. var $users;
  300. /**
  301. * Multisite Blogs table
  302. *
  303. * @since 3.0.0
  304. * @access public
  305. * @var string
  306. */
  307. var $blogs;
  308. /**
  309. * Multisite Blog Versions table
  310. *
  311. * @since 3.0.0
  312. * @access public
  313. * @var string
  314. */
  315. var $blog_versions;
  316. /**
  317. * Multisite Registration Log table
  318. *
  319. * @since 3.0.0
  320. * @access public
  321. * @var string
  322. */
  323. var $registration_log;
  324. /**
  325. * Multisite Signups table
  326. *
  327. * @since 3.0.0
  328. * @access public
  329. * @var string
  330. */
  331. var $signups;
  332. /**
  333. * Multisite Sites table
  334. *
  335. * @since 3.0.0
  336. * @access public
  337. * @var string
  338. */
  339. var $site;
  340. /**
  341. * Multisite Sitewide Terms table
  342. *
  343. * @since 3.0.0
  344. * @access public
  345. * @var string
  346. */
  347. var $sitecategories;
  348. /**
  349. * Multisite Site Metadata table
  350. *
  351. * @since 3.0.0
  352. * @access public
  353. * @var string
  354. */
  355. var $sitemeta;
  356. /**
  357. * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
  358. *
  359. * Keys are column names, values are format types: 'ID' => '%d'
  360. *
  361. * @since 2.8.0
  362. * @see wpdb::prepare()
  363. * @see wpdb::insert()
  364. * @see wpdb::update()
  365. * @see wpdb::delete()
  366. * @see wp_set_wpdb_vars()
  367. * @access public
  368. * @var array
  369. */
  370. var $field_types = array();
  371. /**
  372. * Database table columns charset
  373. *
  374. * @since 2.2.0
  375. * @access public
  376. * @var string
  377. */
  378. var $charset;
  379. /**
  380. * Database table columns collate
  381. *
  382. * @since 2.2.0
  383. * @access public
  384. * @var string
  385. */
  386. var $collate;
  387. /**
  388. * Whether to use mysql_real_escape_string
  389. *
  390. * @since 2.8.0
  391. * @access public
  392. * @var bool
  393. */
  394. var $real_escape = false;
  395. /**
  396. * Database Username
  397. *
  398. * @since 2.9.0
  399. * @access private
  400. * @var string
  401. */
  402. var $dbuser;
  403. /**
  404. * A textual description of the last query/get_row/get_var call
  405. *
  406. * @since 3.0.0
  407. * @access public
  408. * @var string
  409. */
  410. var $func_call;
  411. /**
  412. * Whether MySQL is used as the database engine.
  413. *
  414. * Set in WPDB::db_connect() to true, by default. This is used when checking
  415. * against the required MySQL version for WordPress. Normally, a replacement
  416. * database drop-in (db.php) will skip these checks, but setting this to true
  417. * will force the checks to occur.
  418. *
  419. * @since 3.3.0
  420. * @access public
  421. * @var bool
  422. */
  423. public $is_mysql = null;
  424. /**
  425. * Connects to the database server and selects a database
  426. *
  427. * PHP5 style constructor for compatibility with PHP5. Does
  428. * the actual setting up of the class properties and connection
  429. * to the database.
  430. *
  431. * @link http://core.trac.wordpress.org/ticket/3354
  432. * @since 2.0.8
  433. *
  434. * @param string $dbuser MySQL database user
  435. * @param string $dbpassword MySQL database password
  436. * @param string $dbname MySQL database name
  437. * @param string $dbhost MySQL database host
  438. */
  439. function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
  440. register_shutdown_function( array( &$this, '__destruct' ) );
  441. if ( WP_DEBUG )
  442. $this->show_errors();
  443. $this->init_charset();
  444. $this->dbuser = $dbuser;
  445. $this->dbpassword = $dbpassword;
  446. $this->dbname = $dbname;
  447. $this->dbhost = $dbhost;
  448. $this->db_connect();
  449. }
  450. /**
  451. * PHP5 style destructor and will run when database object is destroyed.
  452. *
  453. * @see wpdb::__construct()
  454. * @since 2.0.8
  455. * @return bool true
  456. */
  457. function __destruct() {
  458. return true;
  459. }
  460. /**
  461. * Set $this->charset and $this->collate
  462. *
  463. * @since 3.1.0
  464. */
  465. function init_charset() {
  466. if ( function_exists('is_multisite') && is_multisite() ) {
  467. $this->charset = 'utf8';
  468. if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
  469. $this->collate = DB_COLLATE;
  470. else
  471. $this->collate = 'utf8_general_ci';
  472. } elseif ( defined( 'DB_COLLATE' ) ) {
  473. $this->collate = DB_COLLATE;
  474. }
  475. if ( defined( 'DB_CHARSET' ) )
  476. $this->charset = DB_CHARSET;
  477. }
  478. /**
  479. * Sets the connection's character set.
  480. *
  481. * @since 3.1.0
  482. *
  483. * @param resource $dbh The resource given by mysql_connect
  484. * @param string $charset The character set (optional)
  485. * @param string $collate The collation (optional)
  486. */
  487. function set_charset($dbh, $charset = null, $collate = null) {
  488. if ( !isset($charset) )
  489. $charset = $this->charset;
  490. if ( !isset($collate) )
  491. $collate = $this->collate;
  492. if ( $this->has_cap( 'collation', $dbh ) && !empty( $charset ) ) {
  493. if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset', $dbh ) ) {
  494. mysql_set_charset( $charset, $dbh );
  495. $this->real_escape = true;
  496. } else {
  497. $query = $this->prepare( 'SET NAMES %s', $charset );
  498. if ( ! empty( $collate ) )
  499. $query .= $this->prepare( ' COLLATE %s', $collate );
  500. mysql_query( $query, $dbh );
  501. }
  502. }
  503. }
  504. /**
  505. * Sets the table prefix for the WordPress tables.
  506. *
  507. * @since 2.5.0
  508. *
  509. * @param string $prefix Alphanumeric name for the new prefix.
  510. * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not.
  511. * @return string|WP_Error Old prefix or WP_Error on error
  512. */
  513. function set_prefix( $prefix, $set_table_names = true ) {
  514. if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
  515. return new WP_Error('invalid_db_prefix', 'Invalid database prefix' );
  516. $old_prefix = is_multisite() ? '' : $prefix;
  517. if ( isset( $this->base_prefix ) )
  518. $old_prefix = $this->base_prefix;
  519. $this->base_prefix = $prefix;
  520. if ( $set_table_names ) {
  521. foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
  522. $this->$table = $prefixed_table;
  523. if ( is_multisite() && empty( $this->blogid ) )
  524. return $old_prefix;
  525. $this->prefix = $this->get_blog_prefix();
  526. foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
  527. $this->$table = $prefixed_table;
  528. foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
  529. $this->$table = $prefixed_table;
  530. }
  531. return $old_prefix;
  532. }
  533. /**
  534. * Sets blog id.
  535. *
  536. * @since 3.0.0
  537. * @access public
  538. * @param int $blog_id
  539. * @param int $site_id Optional.
  540. * @return string previous blog id
  541. */
  542. function set_blog_id( $blog_id, $site_id = 0 ) {
  543. if ( ! empty( $site_id ) )
  544. $this->siteid = $site_id;
  545. $old_blog_id = $this->blogid;
  546. $this->blogid = $blog_id;
  547. $this->prefix = $this->get_blog_prefix();
  548. foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
  549. $this->$table = $prefixed_table;
  550. foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
  551. $this->$table = $prefixed_table;
  552. return $old_blog_id;
  553. }
  554. /**
  555. * Gets blog prefix.
  556. *
  557. * @uses is_multisite()
  558. * @since 3.0.0
  559. * @param int $blog_id Optional.
  560. * @return string Blog prefix.
  561. */
  562. function get_blog_prefix( $blog_id = null ) {
  563. if ( is_multisite() ) {
  564. if ( null === $blog_id )
  565. $blog_id = $this->blogid;
  566. $blog_id = (int) $blog_id;
  567. if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
  568. return $this->base_prefix;
  569. else
  570. return $this->base_prefix . $blog_id . '_';
  571. } else {
  572. return $this->base_prefix;
  573. }
  574. }
  575. /**
  576. * Returns an array of WordPress tables.
  577. *
  578. * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to
  579. * override the WordPress users and usermeta tables that would otherwise
  580. * be determined by the prefix.
  581. *
  582. * The scope argument can take one of the following:
  583. *
  584. * 'all' - returns 'all' and 'global' tables. No old tables are returned.
  585. * 'blog' - returns the blog-level tables for the queried blog.
  586. * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite.
  587. * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
  588. * 'old' - returns tables which are deprecated.
  589. *
  590. * @since 3.0.0
  591. * @uses wpdb::$tables
  592. * @uses wpdb::$old_tables
  593. * @uses wpdb::$global_tables
  594. * @uses wpdb::$ms_global_tables
  595. * @uses is_multisite()
  596. *
  597. * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all.
  598. * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog
  599. * prefix is requested, then the custom users and usermeta tables will be mapped.
  600. * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested.
  601. * @return array Table names. When a prefix is requested, the key is the unprefixed table name.
  602. */
  603. function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
  604. switch ( $scope ) {
  605. case 'all' :
  606. $tables = array_merge( $this->global_tables, $this->tables );
  607. if ( is_multisite() )
  608. $tables = array_merge( $tables, $this->ms_global_tables );
  609. break;
  610. case 'blog' :
  611. $tables = $this->tables;
  612. break;
  613. case 'global' :
  614. $tables = $this->global_tables;
  615. if ( is_multisite() )
  616. $tables = array_merge( $tables, $this->ms_global_tables );
  617. break;
  618. case 'ms_global' :
  619. $tables = $this->ms_global_tables;
  620. break;
  621. case 'old' :
  622. $tables = $this->old_tables;
  623. break;
  624. default :
  625. return array();
  626. break;
  627. }
  628. if ( $prefix ) {
  629. if ( ! $blog_id )
  630. $blog_id = $this->blogid;
  631. $blog_prefix = $this->get_blog_prefix( $blog_id );
  632. $base_prefix = $this->base_prefix;
  633. $global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
  634. foreach ( $tables as $k => $table ) {
  635. if ( in_array( $table, $global_tables ) )
  636. $tables[ $table ] = $base_prefix . $table;
  637. else
  638. $tables[ $table ] = $blog_prefix . $table;
  639. unset( $tables[ $k ] );
  640. }
  641. if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
  642. $tables['users'] = CUSTOM_USER_TABLE;
  643. if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
  644. $tables['usermeta'] = CUSTOM_USER_META_TABLE;
  645. }
  646. return $tables;
  647. }
  648. /**
  649. * Selects a database using the current database connection.
  650. *
  651. * The database name will be changed based on the current database
  652. * connection. On failure, the execution will bail and display an DB error.
  653. *
  654. * @since 0.71
  655. *
  656. * @param string $db MySQL database name
  657. * @param resource $dbh Optional link identifier.
  658. * @return null Always null.
  659. */
  660. function select( $db, $dbh = null ) {
  661. if ( is_null($dbh) )
  662. $dbh = $this->dbh;
  663. if ( !@mysql_select_db( $db, $dbh ) ) {
  664. $this->ready = false;
  665. wp_load_translations_early();
  666. $this->bail( sprintf( __( '<h1>Can&#8217;t select database</h1>
  667. <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p>
  668. <ul>
  669. <li>Are you sure it exists?</li>
  670. <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li>
  671. <li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li>
  672. </ul>
  673. <p>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="http://wordpress.org/support/">WordPress Support Forums</a>.</p>' ), htmlspecialchars( $db, ENT_QUOTES ), htmlspecialchars( $this->dbuser, ENT_QUOTES ) ), 'db_select_fail' );
  674. return;
  675. }
  676. }
  677. /**
  678. * Weak escape, using addslashes()
  679. *
  680. * @see addslashes()
  681. * @since 2.8.0
  682. * @access private
  683. *
  684. * @param string $string
  685. * @return string
  686. */
  687. function _weak_escape( $string ) {
  688. return addslashes( $string );
  689. }
  690. /**
  691. * Real escape, using mysql_real_escape_string() or addslashes()
  692. *
  693. * @see mysql_real_escape_string()
  694. * @see addslashes()
  695. * @since 2.8.0
  696. * @access private
  697. *
  698. * @param string $string to escape
  699. * @return string escaped
  700. */
  701. function _real_escape( $string ) {
  702. if ( $this->dbh && $this->real_escape )
  703. return mysql_real_escape_string( $string, $this->dbh );
  704. else
  705. return addslashes( $string );
  706. }
  707. /**
  708. * Escape data. Works on arrays.
  709. *
  710. * @uses wpdb::_escape()
  711. * @uses wpdb::_real_escape()
  712. * @since 2.8.0
  713. * @access private
  714. *
  715. * @param string|array $data
  716. * @return string|array escaped
  717. */
  718. function _escape( $data ) {
  719. if ( is_array( $data ) ) {
  720. foreach ( (array) $data as $k => $v ) {
  721. if ( is_array($v) )
  722. $data[$k] = $this->_escape( $v );
  723. else
  724. $data[$k] = $this->_real_escape( $v );
  725. }
  726. } else {
  727. $data = $this->_real_escape( $data );
  728. }
  729. return $data;
  730. }
  731. /**
  732. * Escapes content for insertion into the database using addslashes(), for security.
  733. *
  734. * Works on arrays.
  735. *
  736. * @since 0.71
  737. * @param string|array $data to escape
  738. * @return string|array escaped as query safe string
  739. */
  740. function escape( $data ) {
  741. if ( is_array( $data ) ) {
  742. foreach ( (array) $data as $k => $v ) {
  743. if ( is_array( $v ) )
  744. $data[$k] = $this->escape( $v );
  745. else
  746. $data[$k] = $this->_weak_escape( $v );
  747. }
  748. } else {
  749. $data = $this->_weak_escape( $data );
  750. }
  751. return $data;
  752. }
  753. /**
  754. * Escapes content by reference for insertion into the database, for security
  755. *
  756. * @uses wpdb::_real_escape()
  757. * @since 2.3.0
  758. * @param string $string to escape
  759. * @return void
  760. */
  761. function escape_by_ref( &$string ) {
  762. $string = $this->_real_escape( $string );
  763. }
  764. /**
  765. * Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
  766. *
  767. * The following directives can be used in the query format string:
  768. * %d (integer)
  769. * %f (float)
  770. * %s (string)
  771. * %% (literal percentage sign - no argument needed)
  772. *
  773. * All of %d, %f, and %s are to be left unquoted in the query string and they need an argument passed for them.
  774. * Literals (%) as parts of the query must be properly written as %%.
  775. *
  776. * This function only supports a small subset of the sprintf syntax; it only supports %d (integer), %f (float), and %s (string).
  777. * Does not support sign, padding, alignment, width or precision specifiers.
  778. * Does not support argument numbering/swapping.
  779. *
  780. * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}.
  781. *
  782. * Both %d and %s should be left unquoted in the query string.
  783. *
  784. * <code>
  785. * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 )
  786. * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' );
  787. * </code>
  788. *
  789. * @link http://php.net/sprintf Description of syntax.
  790. * @since 2.3.0
  791. *
  792. * @param string $query Query statement with sprintf()-like placeholders
  793. * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like
  794. * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if
  795. * being called like {@link http://php.net/sprintf sprintf()}.
  796. * @param mixed $args,... further variables to substitute into the query's placeholders if being called like
  797. * {@link http://php.net/sprintf sprintf()}.
  798. * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
  799. * if there was something to prepare
  800. */
  801. function prepare( $query = null ) { // ( $query, *$args )
  802. if ( is_null( $query ) )
  803. return;
  804. $args = func_get_args();
  805. array_shift( $args );
  806. // If args were passed as an array (as in vsprintf), move them up
  807. if ( isset( $args[0] ) && is_array($args[0]) )
  808. $args = $args[0];
  809. $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
  810. $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
  811. $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
  812. array_walk( $args, array( &$this, 'escape_by_ref' ) );
  813. return @vsprintf( $query, $args );
  814. }
  815. /**
  816. * Print SQL/DB error.
  817. *
  818. * @since 0.71
  819. * @global array $EZSQL_ERROR Stores error information of query and error string
  820. *
  821. * @param string $str The error to display
  822. * @return bool False if the showing of errors is disabled.
  823. */
  824. function print_error( $str = '' ) {
  825. global $EZSQL_ERROR;
  826. if ( !$str )
  827. $str = mysql_error( $this->dbh );
  828. $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
  829. if ( $this->suppress_errors )
  830. return false;
  831. wp_load_translations_early();
  832. if ( $caller = $this->get_caller() )
  833. $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller );
  834. else
  835. $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query );
  836. if ( function_exists( 'error_log' )
  837. && ( $log_file = @ini_get( 'error_log' ) )
  838. && ( 'syslog' == $log_file || @is_writable( $log_file ) )
  839. )
  840. @error_log( $error_str );
  841. // Are we showing errors?
  842. if ( ! $this->show_errors )
  843. return false;
  844. // If there is an error then take note of it
  845. if ( is_multisite() ) {
  846. $msg = "WordPress database error: [$str]\n{$this->last_query}\n";
  847. if ( defined( 'ERRORLOGFILE' ) )
  848. error_log( $msg, 3, ERRORLOGFILE );
  849. if ( defined( 'DIEONDBERROR' ) )
  850. wp_die( $msg );
  851. } else {
  852. $str = htmlspecialchars( $str, ENT_QUOTES );
  853. $query = htmlspecialchars( $this->last_query, ENT_QUOTES );
  854. print "<div id='error'>
  855. <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
  856. <code>$query</code></p>
  857. </div>";
  858. }
  859. }
  860. /**
  861. * Enables showing of database errors.
  862. *
  863. * This function should be used only to enable showing of errors.
  864. * wpdb::hide_errors() should be used instead for hiding of errors. However,
  865. * this function can be used to enable and disable showing of database
  866. * errors.
  867. *
  868. * @since 0.71
  869. * @see wpdb::hide_errors()
  870. *
  871. * @param bool $show Whether to show or hide errors
  872. * @return bool Old value for showing errors.
  873. */
  874. function show_errors( $show = true ) {
  875. $errors = $this->show_errors;
  876. $this->show_errors = $show;
  877. return $errors;
  878. }
  879. /**
  880. * Disables showing of database errors.
  881. *
  882. * By default database errors are not shown.
  883. *
  884. * @since 0.71
  885. * @see wpdb::show_errors()
  886. *
  887. * @return bool Whether showing of errors was active
  888. */
  889. function hide_errors() {
  890. $show = $this->show_errors;
  891. $this->show_errors = false;
  892. return $show;
  893. }
  894. /**
  895. * Whether to suppress database errors.
  896. *
  897. * By default database errors are suppressed, with a simple
  898. * call to this function they can be enabled.
  899. *
  900. * @since 2.5.0
  901. * @see wpdb::hide_errors()
  902. * @param bool $suppress Optional. New value. Defaults to true.
  903. * @return bool Old value
  904. */
  905. function suppress_errors( $suppress = true ) {
  906. $errors = $this->suppress_errors;
  907. $this->suppress_errors = (bool) $suppress;
  908. return $errors;
  909. }
  910. /**
  911. * Kill cached query results.
  912. *
  913. * @since 0.71
  914. * @return void
  915. */
  916. function flush() {
  917. $this->last_result = array();
  918. $this->col_info = null;
  919. $this->last_query = null;
  920. }
  921. /**
  922. * Connect to and select database
  923. *
  924. * @since 3.0.0
  925. */
  926. function db_connect() {
  927. $this->is_mysql = true;
  928. if ( WP_DEBUG ) {
  929. $this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
  930. } else {
  931. $this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, true );
  932. }
  933. if ( !$this->dbh ) {
  934. wp_load_translations_early();
  935. $this->bail( sprintf( __( "
  936. <h1>Error establishing a database connection</h1>
  937. <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>
  938. <ul>
  939. <li>Are you sure you have the correct username and password?</li>
  940. <li>Are you sure that you have typed the correct hostname?</li>
  941. <li>Are you sure that the database server is running?</li>
  942. </ul>
  943. <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>
  944. " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' );
  945. return;
  946. }
  947. $this->set_charset( $this->dbh );
  948. $this->ready = true;
  949. $this->select( $this->dbname, $this->dbh );
  950. }
  951. /**
  952. * Perform a MySQL database query, using current database connection.
  953. *
  954. * More information can be found on the codex page.
  955. *
  956. * @since 0.71
  957. *
  958. * @param string $query Database query
  959. * @return int|false Number of rows affected/selected or false on error
  960. */
  961. function query( $query ) {
  962. if ( ! $this->ready )
  963. return false;
  964. // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
  965. $query = apply_filters( 'query', $query );
  966. $return_val = 0;
  967. $this->flush();
  968. // Log how the function was called
  969. $this->func_call = "\$db->query(\"$query\")";
  970. // Keep track of the last query for debug..
  971. $this->last_query = $query;
  972. if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
  973. $this->timer_start();
  974. $this->result = @mysql_query( $query, $this->dbh );
  975. $this->num_queries++;
  976. if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
  977. $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
  978. // If there is an error then take note of it..
  979. if ( $this->last_error = mysql_error( $this->dbh ) ) {
  980. $this->print_error();
  981. return false;
  982. }
  983. if ( preg_match( '/^\s*(create|alter|truncate|drop) /i', $query ) ) {
  984. $return_val = $this->result;
  985. } elseif ( preg_match( '/^\s*(insert|delete|update|replace) /i', $query ) ) {
  986. $this->rows_affected = mysql_affected_rows( $this->dbh );
  987. // Take note of the insert_id
  988. if ( preg_match( '/^\s*(insert|replace) /i', $query ) ) {
  989. $this->insert_id = mysql_insert_id($this->dbh);
  990. }
  991. // Return number of rows affected
  992. $return_val = $this->rows_affected;
  993. } else {
  994. $i = 0;
  995. while ( $i < @mysql_num_fields( $this->result ) ) {
  996. $this->col_info[$i] = @mysql_fetch_field( $this->result );
  997. $i++;
  998. }
  999. $num_rows = 0;
  1000. while ( $row = @mysql_fetch_object( $this->result ) ) {
  1001. $this->last_result[$num_rows] = $row;
  1002. $num_rows++;
  1003. }
  1004. @mysql_free_result( $this->result );
  1005. // Log number of rows the query returned
  1006. // and return number of rows selected
  1007. $this->num_rows = $num_rows;
  1008. $return_val = $num_rows;
  1009. }
  1010. return $return_val;
  1011. }
  1012. /**
  1013. * Insert a row into a table.
  1014. *
  1015. * <code>
  1016. * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  1017. * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  1018. * </code>
  1019. *
  1020. * @since 2.5.0
  1021. * @see wpdb::prepare()
  1022. * @see wpdb::$field_types
  1023. * @see wp_set_wpdb_vars()
  1024. *
  1025. * @param string $table table name
  1026. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1027. * @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.
  1028. * 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.
  1029. * @return int|false The number of rows inserted, or false on error.
  1030. */
  1031. function insert( $table, $data, $format = null ) {
  1032. return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' );
  1033. }
  1034. /**
  1035. * Replace a row into a table.
  1036. *
  1037. * <code>
  1038. * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) )
  1039. * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) )
  1040. * </code>
  1041. *
  1042. * @since 3.0.0
  1043. * @see wpdb::prepare()
  1044. * @see wpdb::$field_types
  1045. * @see wp_set_wpdb_vars()
  1046. *
  1047. * @param string $table table name
  1048. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1049. * @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.
  1050. * 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.
  1051. * @return int|false The number of rows affected, or false on error.
  1052. */
  1053. function replace( $table, $data, $format = null ) {
  1054. return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' );
  1055. }
  1056. /**
  1057. * Helper function for insert and replace.
  1058. *
  1059. * Runs an insert or replace query based on $type argument.
  1060. *
  1061. * @access private
  1062. * @since 3.0.0
  1063. * @see wpdb::prepare()
  1064. * @see wpdb::$field_types
  1065. * @see wp_set_wpdb_vars()
  1066. *
  1067. * @param string $table table name
  1068. * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1069. * @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.
  1070. * 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.
  1071. * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT.
  1072. * @return int|false The number of rows affected, or false on error.
  1073. */
  1074. function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
  1075. if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
  1076. return false;
  1077. $formats = $format = (array) $format;
  1078. $fields = array_keys( $data );
  1079. $formatted_fields = array();
  1080. foreach ( $fields as $field ) {
  1081. if ( !empty( $format ) )
  1082. $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
  1083. elseif ( isset( $this->field_types[$field] ) )
  1084. $form = $this->field_types[$field];
  1085. else
  1086. $form = '%s';
  1087. $formatted_fields[] = $form;
  1088. }
  1089. $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
  1090. return $this->query( $this->prepare( $sql, $data ) );
  1091. }
  1092. /**
  1093. * Update a row in the table
  1094. *
  1095. * <code>
  1096. * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) )
  1097. * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
  1098. * </code>
  1099. *
  1100. * @since 2.5.0
  1101. * @see wpdb::prepare()
  1102. * @see wpdb::$field_types
  1103. * @see wp_set_wpdb_vars()
  1104. *
  1105. * @param string $table table name
  1106. * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
  1107. * @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".
  1108. * @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.
  1109. * 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.
  1110. * @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.
  1111. * @return int|false The number of rows updated, or false on error.
  1112. */
  1113. function update( $table, $data, $where, $format = null, $where_format = null ) {
  1114. if ( ! is_array( $data ) || ! is_array( $where ) )
  1115. return false;
  1116. $formats = $format = (array) $format;
  1117. $bits = $wheres = array();
  1118. foreach ( (array) array_keys( $data ) as $field ) {
  1119. if ( !empty( $format ) )
  1120. $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
  1121. elseif ( isset($this->field_types[$field]) )
  1122. $form = $this->field_types[$field];
  1123. else
  1124. $form = '%s';
  1125. $bits[] = "`$field` = {$form}";
  1126. }
  1127. $where_formats = $where_format = (array) $where_format;
  1128. foreach ( (array) array_keys( $where ) as $field ) {
  1129. if ( !empty( $where_format ) )
  1130. $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
  1131. elseif ( isset( $this->field_types[$field] ) )
  1132. $form = $this->field_types[$field];
  1133. else
  1134. $form = '%s';
  1135. $wheres[] = "`$field` = {$form}";
  1136. }
  1137. $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
  1138. return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
  1139. }
  1140. /**
  1141. * Delete a row in the table
  1142. *
  1143. * <code>
  1144. * wpdb::delete( 'table', array( 'ID' => 1 ) )
  1145. * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) )
  1146. * </code>
  1147. *
  1148. * @since 3.4.0
  1149. * @see wpdb::prepare()
  1150. * @see wpdb::$field_types
  1151. * @see wp_set_wpdb_vars()
  1152. *
  1153. * @param string $table table name
  1154. * @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".
  1155. * @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.
  1156. * @return int|false The number of rows updated, or false on error.
  1157. */
  1158. function delete( $table, $where, $where_format = null ) {
  1159. if ( ! is_array( $where ) )
  1160. return false;
  1161. $bits = $wheres = array();
  1162. $where_formats = $where_format = (array) $where_format;
  1163. foreach ( array_keys( $where ) as $field ) {
  1164. if ( !empty( $where_format ) ) {
  1165. $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
  1166. } elseif ( isset( $this->field_types[ $field ] ) ) {
  1167. $form = $this->field_types[ $field ];
  1168. } else {
  1169. $form = '%s';
  1170. }
  1171. $wheres[] = "$field = $form";
  1172. }
  1173. $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
  1174. return $this->query( $this->prepare( $sql, $where ) );
  1175. }
  1176. /**
  1177. * Retrieve one variable from the database.
  1178. *
  1179. * Executes a SQL query and returns the value from the SQL result.
  1180. * 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.
  1181. * If $query is null, this function returns the value in the specified column and row from the previous SQL result.
  1182. *
  1183. * @since 0.71
  1184. *
  1185. * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query.
  1186. * @param int $x Optional. Column of value to return. Indexed from 0.
  1187. * @param int $y Optional. Row of value to return. Indexed from 0.
  1188. * @return string|null Database query result (as string), or null on failure
  1189. */
  1190. function get_var( $query = null, $x = 0, $y = 0 ) {
  1191. $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
  1192. if ( $query )
  1193. $this->query( $query );
  1194. // Extract var out of cached results based x,y vals
  1195. if ( !empty( $this->last_result[$y] ) ) {
  1196. $values = array_values( get_object_vars( $this->last_result[$y] ) );
  1197. }
  1198. // If there is a value return it else return null
  1199. return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
  1200. }
  1201. /**
  1202. * Retrieve one row from the database.
  1203. *
  1204. * Executes a SQL query and returns the row from the SQL result.
  1205. *
  1206. * @since 0.71
  1207. *
  1208. * @param string|null $query SQL query.
  1209. * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...),
  1210. * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
  1211. * @param int $y Optional. Row to return. Indexed from 0.
  1212. * @return mixed Database query result in format specified by $output or null on failure
  1213. */
  1214. function get_row( $query = null, $output = OBJECT, $y = 0 ) {
  1215. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  1216. if ( $query )
  1217. $this->query( $query );
  1218. else
  1219. return null;
  1220. if ( !isset( $this->last_result[$y] ) )
  1221. return null;
  1222. if ( $output == OBJECT ) {
  1223. return $this->last_result[$y] ? $this->last_result[$y] : null;
  1224. } elseif ( $output == ARRAY_A ) {
  1225. return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
  1226. } elseif ( $output == ARRAY_N ) {
  1227. return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
  1228. } else {
  1229. $this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" );
  1230. }
  1231. }
  1232. /**
  1233. * Retrieve one column from the database.
  1234. *
  1235. * Executes a SQL query and returns the column from the SQL result.
  1236. * If the SQL result contains more than one column, this function returns the column specified.
  1237. * If $query is null, this function returns the specified column from the previous SQL result.
  1238. *
  1239. * @since 0.71
  1240. *
  1241. * @param string|null $query Optional. SQL query. Defaults to previous query.
  1242. * @param int $x Optional. Column to return. Indexed from 0.
  1243. * @return array Database query result. Array indexed from 0 by SQL result row number.
  1244. */
  1245. function get_col( $query = null , $x = 0 ) {
  1246. if ( $query )
  1247. $this->query( $query );
  1248. $new_array = array();
  1249. // Extract the column values
  1250. for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
  1251. $new_array[$i] = $this->get_var( null, $x, $i );
  1252. }
  1253. return $new_array;
  1254. }
  1255. /**
  1256. * Retrieve an entire SQL result set from the database (i.e., many rows)
  1257. *
  1258. * Executes a SQL query and returns the entire SQL result.
  1259. *
  1260. * @since 0.71
  1261. *
  1262. * @param string $query SQL query.
  1263. * @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.
  1264. * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively.
  1265. * 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.
  1266. * @return mixed Database query results
  1267. */
  1268. function get_results( $query = null, $output = OBJECT ) {
  1269. $this->func_call = "\$db->get_results(\"$query\", $output)";
  1270. if ( $query )
  1271. $this->query( $query );
  1272. else
  1273. return null;
  1274. $new_array = array();
  1275. if ( $output == OBJECT ) {
  1276. // Return an integer-keyed array of row objects
  1277. return $this->last_result;
  1278. } elseif ( $output == OBJECT_K ) {
  1279. // Return an array of row objects with keys from column 1
  1280. // (Duplicates are discarded)
  1281. foreach ( $this->last_result as $row ) {
  1282. $var_by_ref = get_object_vars( $row );
  1283. $key = array_shift( $var_by_ref );
  1284. if ( ! isset( $new_array[ $key ] ) )
  1285. $new_array[ $key ] = $row;
  1286. }
  1287. return $new_array;
  1288. } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
  1289. // Return an integer-keyed array of...
  1290. if ( $this->last_result ) {
  1291. foreach( (array) $this->last_result as $row ) {
  1292. if ( $output == ARRAY_N ) {
  1293. // ...integer-keyed row arrays
  1294. $new_array[] = array_values( get_object_vars( $row ) );
  1295. } else {
  1296. // ...column name-keyed row arrays
  1297. $new_array[] = get_object_vars( $row );
  1298. }
  1299. }
  1300. }
  1301. return $new_array;
  1302. }
  1303. return null;
  1304. }
  1305. /**
  1306. * Retrieve column metadata from the last query.
  1307. *
  1308. * @since 0.71
  1309. *
  1310. * @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
  1311. * @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
  1312. * @return mixed Column Results
  1313. */
  1314. function get_col_info( $info_type = 'name', $col_offset = -1 ) {
  1315. if ( $this->col_info ) {
  1316. if ( $col_offset == -1 ) {
  1317. $i = 0;
  1318. $new_array = array();
  1319. foreach( (array) $this->col_info as $col ) {
  1320. $new_array[$i] = $col->{$info_type};
  1321. $i++;
  1322. }
  1323. return $new_array;
  1324. } else {
  1325. return $this->col_info[$col_offset]->{$info_type};
  1326. }
  1327. }
  1328. }
  1329. /**
  1330. * Starts the timer, for debugging purposes.
  1331. *
  1332. * @since 1.5.0
  1333. *
  1334. * @return true
  1335. */
  1336. function timer_start() {
  1337. $this->time_start = microtime( true );
  1338. return true;
  1339. }
  1340. /**
  1341. * Stops the debugging timer.
  1342. *
  1343. * @since 1.5.0
  1344. *
  1345. * @return float Total time spent on the query, in seconds
  1346. */
  1347. function timer_stop() {
  1348. return ( microtime( true ) - $this->time_start );
  1349. }
  1350. /**
  1351. * Wraps errors in a nice header and footer and dies.
  1352. *
  1353. * Will not die if wpdb::$show_errors is true
  1354. *
  1355. * @since 1.5.0
  1356. *
  1357. * @param string $message The Error message
  1358. * @param string $error_code Optional. A Computer readable string to identify the error.
  1359. * @return false|void
  1360. */
  1361. function bail( $message, $error_code = '500' ) {
  1362. if ( !$this->show_errors ) {
  1363. if ( class_exists( 'WP_Error' ) )
  1364. $this->error = new WP_Error($error_code, $message);
  1365. else
  1366. $this->error = $message;
  1367. return false;
  1368. }
  1369. wp_die($message);
  1370. }
  1371. /**
  1372. * Whether MySQL database is at least the required minimum version.
  1373. *
  1374. * @since 2.5.0
  1375. * @uses $wp_version
  1376. * @uses $required_mysql_version
  1377. *
  1378. * @return WP_Error
  1379. */
  1380. function check_database_version() {
  1381. global $wp_version, $required_mysql_version;
  1382. // Make sure the server has the required MySQL version
  1383. if ( version_compare($this->db_version(), $required_mysql_version, '<') )
  1384. return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
  1385. }
  1386. /**
  1387. * Whether the database supports collation.
  1388. *
  1389. * Called when WordPress is generating the table scheme.
  1390. *
  1391. * @since 2.5.0
  1392. *
  1393. * @return bool True if collation is supported, false if version does not
  1394. */
  1395. function supports_collation() {
  1396. return $this->has_cap( 'collation' );
  1397. }
  1398. /**
  1399. * Determine if a database supports a particular feature
  1400. *
  1401. * @since 2.7.0
  1402. * @see wpdb::db_version()
  1403. *
  1404. * @param string $db_cap the feature
  1405. * @return bool
  1406. */
  1407. function has_cap( $db_cap ) {
  1408. $version = $this->db_version();
  1409. switch ( strtolower( $db_cap ) ) {
  1410. case 'collation' : // @since 2.5.0
  1411. case 'group_concat' : // @since 2.7
  1412. case 'subqueries' : // @since 2.7
  1413. return version_compare( $version, '4.1', '>=' );
  1414. case 'set_charset' :
  1415. return version_compare($version, '5.0.7', '>=');
  1416. };
  1417. return false;
  1418. }
  1419. /**
  1420. * Retrieve the name of the function that called wpdb.
  1421. *
  1422. * Searches up the list of functions until it reaches
  1423. * the one that would most logically had called this method.
  1424. *
  1425. * @since 2.5.0
  1426. *
  1427. * @return string The name of the calling function
  1428. */
  1429. function get_caller() {
  1430. return wp_debug_backtrace_summary( __CLASS__ );
  1431. }
  1432. /**
  1433. * The database version number.
  1434. *
  1435. * @since 2.7.0
  1436. *
  1437. * @return false|string false on failure, version number on success
  1438. */
  1439. function db_version() {
  1440. return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
  1441. }
  1442. }