PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/php/wp-includes/wp-db.php

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