Wordpress:如何在使用$ wpdb-> get_results时解析结果? [英] Wordpress: How to unescape the results when using $wpdb->get_results?
问题描述
要向数据库添加新行,我使用 $ wpdb-> insert
,并获取使用 $ wpdb-> ; get_results
。
To add a new rows to the database I use $wpdb->insert
, and to get the rows I use $wpdb->get_results
.
问题是 $ wpdb-> insert
似乎逃避输入。例如, ab
在数据库中保存为 a \b
。但是, $ wpdb-> get_results
似乎不会将 a\b
$ c> ab 。
The problem is that $wpdb->insert
seems to be escaping the input. For example, a"b
is saved as a\"b
in the database. But, $wpdb->get_results
doesn't seem to unescape back a\"b
to a"b
.
这是设计的正确行为吗?
Is this the correct behavior by design?
我应该手动解除 $ wpdb-> get_results
的结果吗? (这是什么正确的功能?)
Should I unescape the result of $wpdb->get_results
manually? (What is the proper function for this?)
推荐答案
$ wpdb-> insert() / code>和
$ wpdb-> prepare()
将转义数据,以防止 SQL注入攻击。 $ wpdb-> get_results()
函数旨在一般地运行SQL SELECT
语句,所以我相信事实上,斜线被留在原地是有意的。这使得消费者可以根据需要处理数据。
$wpdb->insert()
and $wpdb->prepare()
will escape data to prevent SQL injection attacks. The $wpdb->get_results()
function is designed to work generically with SQL SELECT
statements, so I believe the fact that the slashes are left in place is intentional. This allows the consumer of the data to process it as necessary.
由于 $ wpdb-> get_results()
funciton返回一个 stdClass
对象的数组,为了删除每行中所有列的斜杠,必须遍历行,并遍历每个行对象的属性运行PHP stripslashes()
函数。
Since the $wpdb->get_results()
funciton returns an array of stdClass
objects, in order to remove the slashes in all columns in every row, you must iterate through the rows, and through the properties of each row object running the PHP stripslashes()
function on it.
foreach( $quotes as &$quote ) {
foreach( $quote as &$field ) {
if ( is_string( $field ) )
$field = stripslashes( $field );
}
}
有关wpdb-> get_results()函数的更多信息:
More information on the wpdb->get_results() function: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
这篇关于Wordpress:如何在使用$ wpdb-> get_results时解析结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!