wpdb::_real_escape() WordPress Method

The wpdb::_real_escape() method is used to escape a string for use in a SQL query. This is useful when you need to make sure that a string is safe to use in a query, without having to worry about SQL injection attacks.

wpdb::_real_escape( string $string ) #

Real escape, using mysqli_real_escape_string() or mysql_real_escape_string().


Description

Top ↑

See also


Top ↑

Parameters

$string

(string)(Required)String to escape.


Top ↑

Return

(string) Escaped string.


Top ↑

Source

File: wp-includes/wp-db.php

	public function _real_escape( $string ) {
		if ( ! is_scalar( $string ) ) {
			return '';
		}

		if ( $this->dbh ) {
			if ( $this->use_mysqli ) {
				$escaped = mysqli_real_escape_string( $this->dbh, $string );
			} else {
				$escaped = mysql_real_escape_string( $string, $this->dbh );
			}
		} else {
			$class = get_class( $this );

			wp_load_translations_early();
			/* translators: %s: Database access abstraction class, usually wpdb or a class extending wpdb. */
			_doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' );

			$escaped = addslashes( $string );
		}

		return $this->add_placeholder_escape( $escaped );
	}


Top ↑

Changelog

Changelog
VersionDescription
2.8.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.