admin管理员组文章数量:1278854
I trying show data from my database. This data was escape by esc_sql like bellow.
But when I use it in echo
, line-returns are print as normal chars ('\', 'r', '\', 'n').
$data = esc_sql("
sample data with line returns
");
echo $data; // Output = \r\n sample data with line returns\r\n
What is the best way to re-apply line returns after doing esc_sql ?
I trying show data from my database. This data was escape by esc_sql like bellow.
But when I use it in echo
, line-returns are print as normal chars ('\', 'r', '\', 'n').
$data = esc_sql("
sample data with line returns
");
echo $data; // Output = \r\n sample data with line returns\r\n
What is the best way to re-apply line returns after doing esc_sql ?
Share Improve this question edited Nov 8, 2021 at 15:42 Adrien Villalonga asked Nov 8, 2021 at 15:36 Adrien VillalongaAdrien Villalonga 1838 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 2That's not what esc_sql
is for, or what it should be used for.
Safely Escaping Variables In SQL Queries
To make variables safe for an SQL query, use $wpdb->prepare
, e.g.
$table_name = "{$wpdb->prefix}myTable";
$myID = 12;
$wpdb->query(
$wpdb->prepare(
"UPDATE `$table_name` SET `your_column_1` = 1 WHERE `$table_name`.`your_column_id` = %d",
$myID
)
);
Notice that $myID
is safely inserted into the SQL query using $wpdb->prepare
, and is not directly included in the query string. esc_sql
is not used here.
Safely Escaping Query Results
To escape data you have retrieved from the database on output, use the function that matches the context it's being displayed in
esc_html
for non-HTML textesc_attr
for HTML tag attributesesc_url
for URLswp_kses_post
for content with tags allowed in post content
When Should I Use esc_sql
?
Very, very, rarely:
Usually you should prepare queries using wpdb::prepare(). Sometimes, spot-escaping is required or useful. One example is preparing an array for use in an IN clause.
and
Be careful in using 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.
https://developer.wordpress/reference/functions/esc_sql/
Most WP developers will never use or encounter esc_sql
and that's a good thing.
本文标签: sqlWhy line returns are not reapply after doing escsql
版权声明:本文标题:sql - Why line returns are not reapply after doing esc_sql? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741230412a2362008.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wpdb->get_results
the result is same – Adrien Villalonga Commented Nov 8, 2021 at 15:47BasicExample
values are escape too – Adrien Villalonga Commented Nov 8, 2021 at 15:55