Title: wpdb::check_safe_collation
Published: April 23, 2015
Last modified: February 24, 2026

---

# wpdb::check_safe_collation( string $query ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#wp--skip-link--target)

Checks if the query is accessing a collation considered safe.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#parameters)󠁿

 `$query`stringrequired

The query to check.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#return)󠁿

 bool True if the collation is safe, false if it isn’t.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#source)󠁿

    ```php
    protected function check_safe_collation( $query ) {
    	if ( $this->checking_collation ) {
    		return true;
    	}

    	// We don't need to check the collation for queries that don't read data.
    	$query = ltrim( $query, "\r\n\t (" );
    	if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $query ) ) {
    		return true;
    	}

    	// All-ASCII queries don't need extra checking.
    	if ( $this->check_ascii( $query ) ) {
    		return true;
    	}

    	$table = $this->get_table_from_query( $query );
    	if ( ! $table ) {
    		return false;
    	}

    	$this->checking_collation = true;
    	$collation                = $this->get_table_charset( $table );
    	$this->checking_collation = false;

    	// Tables with no collation, or latin1 only, don't need extra checking.
    	if ( false === $collation || 'latin1' === $collation ) {
    		return true;
    	}

    	$table = strtolower( $table );
    	if ( empty( $this->col_meta[ $table ] ) ) {
    		return false;
    	}

    	// If any of the columns don't have one of these collations, it needs more confidence checking.
    	$safe_collations = array(
    		'utf8_bin',
    		'utf8_general_ci',
    		'utf8mb3_bin',
    		'utf8mb3_general_ci',
    		'utf8mb4_bin',
    		'utf8mb4_general_ci',
    	);

    	foreach ( $this->col_meta[ $table ] as $col ) {
    		if ( empty( $col->Collation ) ) {
    			continue;
    		}

    		if ( ! in_array( $col->Collation, $safe_collations, true ) ) {
    			return false;
    		}
    	}

    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wpdb.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wpdb.php#L3489)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wpdb.php#L3489-L3545)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::get_table_from_query()](https://developer.wordpress.org/reference/classes/wpdb/get_table_from_query/)`wp-includes/class-wpdb.php` |

Finds the first table name referenced in a query.

  | 
| [wpdb::check_ascii()](https://developer.wordpress.org/reference/classes/wpdb/check_ascii/)`wp-includes/class-wpdb.php` |

Checks if a string is ASCII.

  | 
| [wpdb::get_table_charset()](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/)`wp-includes/class-wpdb.php` |

Retrieves the character set for the given table.

  |

| Used by | Description | 
| [wpdb::get_var()](https://developer.wordpress.org/reference/classes/wpdb/get_var/)`wp-includes/class-wpdb.php` |

Retrieves one value from the database.

  | 
| [wpdb::get_row()](https://developer.wordpress.org/reference/classes/wpdb/get_row/)`wp-includes/class-wpdb.php` |

Retrieves one row from the database.

  | 
| [wpdb::get_col()](https://developer.wordpress.org/reference/classes/wpdb/get_col/)`wp-includes/class-wpdb.php` |

Retrieves one column from the database.

  | 
| [wpdb::get_results()](https://developer.wordpress.org/reference/classes/wpdb/get_results/)`wp-includes/class-wpdb.php` |

Retrieves an entire SQL result set from the database (i.e., many rows).

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wpdb/check_safe_collation/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.2.0](https://developer.wordpress.org/reference/since/4.2.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwpdb%2Fcheck_safe_collation%2F)
before being able to contribute a note or feedback.