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

/site_v1.0/wordpress/wp-includes/wp-db.php

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