wpdb::bail() WordPress Method

The wpdb::bail() method is used to gracefully bail out of a database operation when an error occurs. This is useful for making sure that Wordpress doesn't try to go ahead and do something that might end up corrupting the database.

wpdb::bail( string $message, string $error_code = '500' ) #

Wraps errors in a nice header and footer and dies.


Description

Will not die if wpdb::$show_errors is false.


Top ↑

Parameters

$message

(string)(Required)The error message.

$error_code

(string)(Optional) A computer-readable string to identify the error.

Default value: '500'


Top ↑

Return

(void|false) Void if the showing of errors is enabled, false if disabled.


Top ↑

Source

File: wp-includes/wp-db.php

	public function bail( $message, $error_code = '500' ) {
		if ( $this->show_errors ) {
			$error = '';

			if ( $this->use_mysqli ) {
				if ( $this->dbh instanceof mysqli ) {
					$error = mysqli_error( $this->dbh );
				} elseif ( mysqli_connect_errno() ) {
					$error = mysqli_connect_error();
				}
			} else {
				if ( is_resource( $this->dbh ) ) {
					$error = mysql_error( $this->dbh );
				} else {
					$error = mysql_error();
				}
			}

			if ( $error ) {
				$message = '<p><code>' . $error . "</code></p>\n" . $message;
			}

			wp_die( $message );
		} else {
			if ( class_exists( 'WP_Error', false ) ) {
				$this->error = new WP_Error( $error_code, $message );
			} else {
				$this->error = $message;
			}

			return false;
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

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.