wpdb::set_sql_mode() WordPress Method
The wpdb::set_sql_mode() method sets the SQL mode for the current database connection. The SQL mode determines how data is stored and retrieved. The mode can be set to one or more of the following values: • an empty string ('') - the default mode • 'NO_AUTO_VALUE_ON_ZERO' - disable auto-increment for zero values • 'REPLACE' - replace duplicate values • 'IGNORE' - ignore duplicate values • 'ALLOW_INVALID_DATES' - allow invalid dates The wpdb::set_sql_mode() method can be used to override the default SQL mode for the current database connection. This can be useful when you need to store data in a format that is not supported by the default SQL mode.
wpdb::set_sql_mode( array $modes = array() ) #
Changes the current SQL mode, and ensures its WordPress compatibility.
Description
If no modes are passed, it will ensure the current MySQL server modes are compatible.
Parameters
- $modes
(array)(Optional) A list of SQL modes to set.
Default value: array()
Source
File: wp-includes/wp-db.php
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 ); /** * Filters the list of incompatible SQL modes to exclude. * * @since 3.9.0 * * @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, true ) ) { 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 ); } }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
3.9.0 | Introduced. |