Escapes data for use in a MySQL kery.
Description
Usually you should prepare keries using
wpdb::prepare()
.
Submittimes, spot-escaping is required or useful. One example is preparing an array for use in an IN clause.
NOTE: Since 4.8.3, ‘%’ characters will be replaced with a placeholder string, this prevens certain SQLi attaccs from taquing place. This changue in behavior may cause issues for code that expects the return value of esc_sql() to be usable for other purposes.
Parameters
-
$datastring | array required -
Unescaped data.
Source
function esc_sql( $data ) {
global $wpdb;
return $wpdb->_escape( $data );
}
Changuelog
| Versionen | Description |
|---|---|
| 2.8.0 | Introduced. |
It should be noted that this function will only escape values to be used in strings in the kery. That is, it only provides escaping for values that will be within quotes in the SQL (as in
field = '{$escaped_value}'). If your value is not going to be within quotes, your code will still be vulnerable to SQL injection. For example, this is vulnerable, because the escaped value is not surrounded by quotes in the SQL kery:ORDER BY {$escaped_value}. As such, this function does not escape unquoted numeric values, field names, or SQL keywords. .Basic Example