Languages: English • Reference/esc sql 日本語 (Add your language)
Prepares a string for use as an SQL query. This function is a glorified addslashes() that works with arrays.
In 99% of cases, you can use $wpdb->prepare() instead, and that is the recommended method. This function is only for use in those rare cases where you can't easily use $wpdb->prepare(). One example is preparing an array for use in an IN clause.
Note: Be careful to use this function correctly. It will only escape values to be used in strings in the query. 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 query: ORDER BY {$escaped_value}. As such, this function does not escape unquoted numeric values, field names, or SQL keywords.
<?php esc_sql( $sql ); ?>
<?php $name = esc_sql( $name ); $status = esc_sql( $status ); $wpdb->get_var( "SELECT something FROM table WHERE foo = '$name' and status = '$status'" ); ?>
Since: 2.8.0
esc_sql() is located in wp-includes/formatting.php
.
like_escape(), tag_escape(), urlencode(), urlencode_deep()
See: Data Validation article for an in-depth discussion of input and output sanitization.