Opened 9 years ago
Closed 9 years ago
#36649 closed defect (bug) (fixed)
Right-hand CAST in a MySQL query produces DB error
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.6 | Priority: | normal |
Severity: | normal | Version: | 2.8 |
Component: | Database | Keywords: | has-patch needs-testing has-unit-tests |
Focuses: | Cc: |
Description
A right-hand CAST like this
<?php add_action( 'init', function() { global $wpdb; $r = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_value=CAST('default' AS CHAR);"); });
produces this error
WordPress database error: [Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation '='] SELECT * FROM wp_postmeta WHERE meta_value=CAST('default' AS CHAR);
Attachments (3)
Change History (10)
#2
@
9 years ago
- Keywords has-patch needs-testing dev-feedback added
In attachment:36649.diff, we can use mysqli_set_charset()
so that mysqli_real_escape_string()
is aware of the character set (see the Caution section in the `mysqli_real_escape_string()` docs), and then set the character set (again) but this time with the collation.
#4
@
9 years ago
- Keywords needs-unit-tests added; dev-feedback removed
- Milestone changed from Awaiting Review to 4.6
#5
@
9 years ago
- Keywords has-unit-tests added; needs-unit-tests removed
Added a test in attachment:36649.2.diff. Is it useful?
Note: See
TracTickets for help on using
tickets.
Before r10597, the collation was always set and still is if
mysqli_set_charset()
is undefined or the version of MySQL is < 5.0.7.Setting charset via
mysql_set_charset()
has been preferred since r10597, presumably to makemysql_real_escape_string()
aware of the MySQL connection charset for charset-specific escaping (see #5455#comment:20) thus avoiding a security vulnerability. However, this does not set the MySQL connection's collation.