PageRenderTime 73ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/msw/dev/wp-includes/wp-db.php

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