+ /**
+ * The number of times to retry reconnecting before dying.
+ *
+ * @since 3.9.0
+ * @access protected
+ * @see wpdb::check_connection()
+ * @var int
+ */
+ protected $reconnect_retries = 5;
+
+ /**
+ * WordPress table prefix
+ *
+ * You can set this to have multiple WordPress installations
+ * in a single database. The second reason is for possible
+ * security precautions.
+ *
+ * @since 2.5.0
+ * @access private
+ * @var string
+ */
+ var $prefix = '';
+
+ /**
+ * WordPress base table prefix.
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $base_prefix;
+
+ /**
+ * Whether the database queries are ready to start executing.
+ *
+ * @since 2.3.2
+ * @access private
+ * @var bool
+ */
+ var $ready = false;
+
+ /**
+ * {@internal Missing Description}}
+ *
+ * @since 3.0.0
+ * @access public
+ * @var int
+ */
+ public $blogid = 0;
+
+ /**
+ * {@internal Missing Description}}
+ *
+ * @since 3.0.0
+ * @access public
+ * @var int
+ */
+ public $siteid = 0;
+
+ /**
+ * List of WordPress per-blog tables
+ *
+ * @since 2.5.0
+ * @access private
+ * @see wpdb::tables()
+ * @var array
+ */
+ var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
+ 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' );
+
+ /**
+ * List of deprecated WordPress tables
+ *
+ * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539
+ *
+ * @since 2.9.0
+ * @access private
+ * @see wpdb::tables()
+ * @var array
+ */
+ var $old_tables = array( 'categories', 'post2cat', 'link2cat' );
+
+ /**
+ * List of WordPress global tables
+ *
+ * @since 3.0.0
+ * @access private
+ * @see wpdb::tables()
+ * @var array
+ */
+ var $global_tables = array( 'users', 'usermeta' );
+
+ /**
+ * List of Multisite global tables
+ *
+ * @since 3.0.0
+ * @access private
+ * @see wpdb::tables()
+ * @var array
+ */
+ var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
+ 'sitecategories', 'registration_log', 'blog_versions' );
+
+ /**
+ * WordPress Comments table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ public $comments;
+
+ /**
+ * WordPress Comment Metadata table
+ *
+ * @since 2.9.0
+ * @access public
+ * @var string
+ */
+ public $commentmeta;
+
+ /**
+ * WordPress Links table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ public $links;
+
+ /**
+ * WordPress Options table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ public $options;
+
+ /**
+ * WordPress Post Metadata table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ public $postmeta;
+
+ /**
+ * WordPress Posts table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ public $posts;
+
+ /**
+ * WordPress Terms table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ public $terms;
+
+ /**
+ * WordPress Term Relationships table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ public $term_relationships;
+
+ /**
+ * WordPress Term Taxonomy table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ public $term_taxonomy;
+
+ /*
+ * Global and Multisite tables
+ */
+
+ /**
+ * WordPress User Metadata table
+ *
+ * @since 2.3.0
+ * @access public
+ * @var string
+ */
+ public $usermeta;
+
+ /**
+ * WordPress Users table
+ *
+ * @since 1.5.0
+ * @access public
+ * @var string
+ */
+ public $users;
+
+ /**
+ * Multisite Blogs table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $blogs;
+
+ /**
+ * Multisite Blog Versions table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $blog_versions;
+
+ /**
+ * Multisite Registration Log table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $registration_log;
+
+ /**
+ * Multisite Signups table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $signups;
+
+ /**
+ * Multisite Sites table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $site;
+
+ /**
+ * Multisite Sitewide Terms table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $sitecategories;
+
+ /**
+ * Multisite Site Metadata table
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $sitemeta;
+
+ /**
+ * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
+ *
+ * Keys are column names, values are format types: 'ID' => '%d'
+ *
+ * @since 2.8.0
+ * @see wpdb::prepare()
+ * @see wpdb::insert()
+ * @see wpdb::update()
+ * @see wpdb::delete()
+ * @see wp_set_wpdb_vars()
+ * @access public
+ * @var array
+ */
+ public $field_types = array();
+
+ /**
+ * Database table columns charset
+ *
+ * @since 2.2.0
+ * @access public
+ * @var string
+ */
+ public $charset;
+
+ /**
+ * Database table columns collate
+ *
+ * @since 2.2.0
+ * @access public
+ * @var string
+ */
+ public $collate;
+
+ /**
+ * Database Username
+ *
+ * @since 2.9.0
+ * @access protected
+ * @var string
+ */
+ protected $dbuser;
+
+ /**
+ * Database Password
+ *
+ * @since 3.1.0
+ * @access protected
+ * @var string
+ */
+ protected $dbpassword;
+
+ /**
+ * Database Name
+ *
+ * @since 3.1.0
+ * @access protected
+ * @var string
+ */
+ protected $dbname;
+
+ /**
+ * Database Host
+ *
+ * @since 3.1.0
+ * @access protected
+ * @var string
+ */
+ protected $dbhost;
+
+ /**
+ * Database Handle
+ *
+ * @since 0.71
+ * @access protected
+ * @var string
+ */
+ protected $dbh;
+
+ /**
+ * A textual description of the last query/get_row/get_var call
+ *
+ * @since 3.0.0
+ * @access public
+ * @var string
+ */
+ public $func_call;
+
+ /**
+ * Whether MySQL is used as the database engine.
+ *
+ * Set in WPDB::db_connect() to true, by default. This is used when checking
+ * against the required MySQL version for WordPress. Normally, a replacement
+ * database drop-in (db.php) will skip these checks, but setting this to true
+ * will force the checks to occur.
+ *
+ * @since 3.3.0
+ * @access public
+ * @var bool
+ */
+ public $is_mysql = null;
+
+ /**
+ * A list of incompatible SQL modes.
+ *
+ * @since 3.9.0
+ * @access protected
+ * @var array
+ */
+ protected $incompatible_modes = array( 'NO_ZERO_DATE', 'ONLY_FULL_GROUP_BY',
+ 'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
+
+ /**
+ * Whether to use mysqli over mysql.
+ *
+ * @since 3.9.0
+ * @access private
+ * @var bool
+ */
+ private $use_mysqli = false;
+
+ /**
+ * Whether we've managed to successfully connect at some point
+ *
+ * @since 3.9.0
+ * @access private
+ * @var bool
+ */
+ private $has_connected = false;
+
+ /**
+ * Connects to the database server and selects a database
+ *
+ * PHP5 style constructor for compatibility with PHP5. Does
+ * the actual setting up of the class properties and connection
+ * to the database.
+ *
+ * @link http://core.trac.wordpress.org/ticket/3354
+ * @since 2.0.8
+ *
+ * @param string $dbuser MySQL database user
+ * @param string $dbpassword MySQL database password
+ * @param string $dbname MySQL database name
+ * @param string $dbhost MySQL database host
+ */
+ public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
+ register_shutdown_function( array( $this, '__destruct' ) );
+
+ if ( WP_DEBUG && WP_DEBUG_DISPLAY )
+ $this->show_errors();
+
+ /* Use ext/mysqli if it exists and:
+ * - WP_USE_EXT_MYSQL is defined as false, or
+ * - We are a development version of WordPress, or
+ * - We are running PHP 5.5 or greater, or
+ * - ext/mysql is not loaded.
+ */
+ if ( function_exists( 'mysqli_connect' ) ) {
+ if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
+ $this->use_mysqli = ! WP_USE_EXT_MYSQL;
+ } elseif ( version_compare( phpversion(), '5.5', '>=' ) || ! function_exists( 'mysql_connect' ) ) {
+ $this->use_mysqli = true;
+ } elseif ( false !== strpos( $GLOBALS['wp_version'], '-' ) ) {
+ $this->use_mysqli = true;
+ }
+ }
+
+ $this->init_charset();
+
+ $this->dbuser = $dbuser;
+ $this->dbpassword = $dbpassword;
+ $this->dbname = $dbname;
+ $this->dbhost = $dbhost;
+
+ // wp-config.php creation will manually connect when ready.
+ if ( defined( 'WP_SETUP_CONFIG' ) ) {
+ return;
+ }
+
+ $this->db_connect();
+ }
+
+ /**
+ * PHP5 style destructor and will run when database object is destroyed.
+ *
+ * @see wpdb::__construct()
+ * @since 2.0.8
+ * @return bool true
+ */
+ public function __destruct() {
+ return true;
+ }
+
+ /**
+ * PHP5 style magic getter, used to lazy-load expensive data.
+ *
+ * @since 3.5.0
+ *
+ * @param string $name The private member to get, and optionally process
+ * @return mixed The private member
+ */
+ public function __get( $name ) {
+ if ( 'col_info' == $name )
+ $this->load_col_info();
+
+ return $this->$name;
+ }
+
+ /**
+ * Magic function, for backwards compatibility.
+ *
+ * @since 3.5.0
+ *
+ * @param string $name The private member to set
+ * @param mixed $value The value to set
+ */
+ public function __set( $name, $value ) {
+ $this->$name = $value;
+ }
+
+ /**
+ * Magic function, for backwards compatibility.
+ *
+ * @since 3.5.0
+ *
+ * @param string $name The private member to check
+ *
+ * @return bool If the member is set or not
+ */
+ public function __isset( $name ) {
+ return isset( $this->$name );
+ }
+
+ /**
+ * Magic function, for backwards compatibility.
+ *
+ * @since 3.5.0
+ *
+ * @param string $name The private member to unset
+ */
+ public function __unset( $name ) {
+ unset( $this->$name );
+ }
+
+ /**
+ * Set $this->charset and $this->collate
+ *
+ * @since 3.1.0
+ */
+ public function init_charset() {
+ if ( function_exists('is_multisite') && is_multisite() ) {
+ $this->charset = 'utf8';
+ if ( defined( 'DB_COLLATE' ) && DB_COLLATE )
+ $this->collate = DB_COLLATE;
+ else
+ $this->collate = 'utf8_general_ci';
+ } elseif ( defined( 'DB_COLLATE' ) ) {
+ $this->collate = DB_COLLATE;
+ }
+
+ if ( defined( 'DB_CHARSET' ) )
+ $this->charset = DB_CHARSET;
+ }
+
+ /**
+ * Sets the connection's character set.
+ *
+ * @since 3.1.0
+ *
+ * @param resource $dbh The resource given by mysql_connect
+ * @param string $charset The character set (optional)
+ * @param string $collate The collation (optional)
+ */
+ public function set_charset( $dbh, $charset = null, $collate = null ) {
+ if ( ! isset( $charset ) )
+ $charset = $this->charset;
+ if ( ! isset( $collate ) )
+ $collate = $this->collate;
+ if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
+ if ( $this->use_mysqli ) {
+ if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
+ mysqli_set_charset( $dbh, $charset );
+ } else {
+ $query = $this->prepare( 'SET NAMES %s', $charset );
+ if ( ! empty( $collate ) )
+ $query .= $this->prepare( ' COLLATE %s', $collate );
+ mysqli_query( $query, $dbh );
+ }
+ } else {
+ if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) {
+ mysql_set_charset( $charset, $dbh );
+ } else {
+ $query = $this->prepare( 'SET NAMES %s', $charset );
+ if ( ! empty( $collate ) )
+ $query .= $this->prepare( ' COLLATE %s', $collate );
+ mysql_query( $query, $dbh );
+ }
+ }
+ }
+ }
+
+ /**
+ * Change the current SQL mode, and ensure its WordPress compatibility.
+ *
+ * If no modes are passed, it will ensure the current MySQL server
+ * modes are compatible.
+ *
+ * @since 3.9.0
+ *
+ * @param array $modes Optional. A list of SQL modes to set.
+ */
+ public function set_sql_mode( $modes = array() ) {
+ if ( empty( $modes ) ) {
+ if ( $this->use_mysqli ) {
+ $res = mysqli_query( $this->dbh, 'SELECT @@SESSION.sql_mode' );
+ } else {
+ $res = mysql_query( 'SELECT @@SESSION.sql_mode', $this->dbh );
+ }
+
+ if ( empty( $res ) ) {
+ return;
+ }
+
+ if ( $this->use_mysqli ) {
+ $modes_array = mysqli_fetch_array( $res );
+ if ( empty( $modes_array[0] ) ) {
+ return;
+ }
+ $modes_str = $modes_array[0];
+ } else {
+ $modes_str = mysql_result( $res, 0 );
+ }
+
+ if ( empty( $modes_str ) ) {
+ return;
+ }
+
+ $modes = explode( ',', $modes_str );
+ }
+
+ $modes = array_change_key_case( $modes, CASE_UPPER );
+
+ /**
+ * Filter the list of incompatible SQL modes to exclude.
+ *
+ * @since 3.9.0
+ *
+ * @see wpdb::$incompatible_modes
+ *
+ * @param array $incompatible_modes An array of incompatible modes.
+ */
+ $incompatible_modes = (array) apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
+
+ foreach( $modes as $i => $mode ) {
+ if ( in_array( $mode, $incompatible_modes ) ) {
+ unset( $modes[ $i ] );
+ }
+ }
+
+ $modes_str = implode( ',', $modes );
+
+ if ( $this->use_mysqli ) {
+ mysqli_query( $this->dbh, "SET SESSION sql_mode='$modes_str'" );
+ } else {
+ mysql_query( "SET SESSION sql_mode='$modes_str'", $this->dbh );