我很难弄清楚是否应该使用$wpdb->prepare
我在 WordPress 中进行数据库查询,以防止 SQL 注入等情况。
The $wpdb 法典 https://codex.wordpress.org/Class_Reference/wpdb显示了一些使用 $wpdb->prepare 函数的示例,以及不使用它的其他示例。
另外,在这个答案 https://stackoverflow.com/a/16040644/849987在 StackOverflow 上,有人提到了一个函数,例如$wpdb->insert
与使用具有相同的安全级别$wpdb->prepare
。但其他的呢$wpdb
函数如$wpdb->get_var
or $wpdb->query
?
我应该什么时候使用$wpdb->prepare
,如果有的话?
我的一些(简体)$wpdb
类和函数的用法如下所示:
示例1: $wpdb->insert
$wpdb->insert(
'special_posts',
array(
'title' => $title,
'selftext_html' => $selftext_html,
'selftext' => $selftext,
),
array(
'%s',
'%s',
'%s',
)
);
示例2: $wpdb->get_results
$wpdb->get_results("SELECT * FROM special_posts WHERE selftext_html = '$value'");
示例3: $wpdb->get_var
$wpdb->get_var("SELECT title FROM special_posts ORDER BY id DESC LIMIT 1");
示例4: $wpdb->query
$wpdb->query('TRUNCATE TABLE special_posts');