wpdb::__construct() WordPress Method

The wpdb::__construct() method sets up the database connection for Wordpress. It takes in the parameters for the database host, database user, database password, and database name. If the connection can not be made, an error is thrown.

wpdb::__construct( string $dbuser, string $dbpassword, string $dbname, string $dbhost ) #

Connects to the database server and selects a database.


Description

Does the actual setting up of the class properties and connection to the database.


Top ↑

Parameters

$dbuser

(string)(Required)Database user.

$dbpassword

(string)(Required)Database password.

$dbname

(string)(Required)Database name.

$dbhost

(string)(Required)Database host.


Top ↑

Source

File: wp-includes/wp-db.php

	public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
		if ( WP_DEBUG && WP_DEBUG_DISPLAY ) {
			$this->show_errors();
		}

		// Use the `mysqli` extension if it exists unless `WP_USE_EXT_MYSQL` is defined as true.
		if ( function_exists( 'mysqli_connect' ) ) {
			$this->use_mysqli = true;

			if ( defined( 'WP_USE_EXT_MYSQL' ) ) {
				$this->use_mysqli = ! WP_USE_EXT_MYSQL;
			}
		}

		$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();
	}


Top ↑

Changelog

Changelog
VersionDescription
2.0.8Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.