PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/wp-db.php

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