Ticket #6410 (closed enhancement: fixed)
DB_COLLATE is not used in database queries
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 2.6 |
| Component: | General | Version: | 2.3 |
| Severity: | normal | Keywords: | |
| Cc: | ryan |
Description
Testing WP-2.5RC1 I found that the collation set in "wp-config.php" (DB_COLLATE) is not being used in searches. I think it should.
This can easily be fixed. In "wp-includes/query.php" the following part can be changed as indicated here:
// If a search pattern is specified, load the posts that match
if ( !empty($q['s']) ) {
// [...]
foreach((array)$q['search_terms'] as $term) {
$term = addslashes_gpc($term);
// The following like has been added
if ($wpdb->collate) $collatesql = ' COLLATE ' . $wpdb->collate;
// The following line has been changed
// $collatesql was inserted in two places
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}'$collatesql) OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'$collatesql))";
$searchand = ' AND ';
}
// [...]
}
There is probably a more general way to fix this, e.g. by including a "COLLATE" statement (using DB_COLLATE) along with "SET NAME" $this->query("SET NAMES '$this->charset'") in "wp-db.php", but the above solution should also work.
Attachments
Change History
-
attachment
patch-for-ticket-6410
added
Added a patch implementing my solution
- Cc ryan added
- Owner changed from anonymous to westi
- Version set to 2.3
- Status changed from new to assigned
- Summary changed from DB_COLLATE is not used in searches to DB_COLLATE is not used in database queries
That does it for trunk.
Ryan can you give this the once over before we possibly merge into 2.5.1
when SAVEQUERIES is turned on, the first query always is:
[0]=>
array(3) {
[0]=>
string(16) "SET NAMES 'utf8'"
[1]=>
float(0.00020217895507812)
[2]=>
string(12) "require_once"
doesn't this mean db_collate is actually used?
D.
doesn't this mean db_collate is actually used?
"SET NAMES 'utf8'" sets the charset to utf8
"SET NAMES 'utf8' COLLATE 'abcde'" sets the charset to utf8 AND the collate to abcde(I'm not sure of a valid collate, so i'm just using that as an example)
Replying to DD32:
doesn't this mean db_collate is actually used?
"SET NAMES 'utf8'" sets the charset to utf8
"SET NAMES 'utf8' COLLATE 'abcde'" sets the charset to utf8 AND the collate to abcde(I'm not sure of a valid collate, so i'm just using that as an example)
This is correct. Collation is rarely needed unless the db charset has many possible collations.

bertilow, can you attach a patch?