drh96824 2019-07-09 00:06
浏览 48
已采纳

我总是听到使用准备好的陈述,但是在wordpress中呢?

Question 1. I know I don't need to do them for wbdb->insert or wbpdb->delete, but people say do it anyway even though the class will do it for you.

// I've got a select count query function and should use it here.

global $wpdb;
$entries = $wpdb->prefix . 'simpledir_entries';
$count = $wpdb->get_var("SELECT COUNT(*) FROM $entries WHERE rel = $id;");

if($count <= 0){
    return false;
}else{
    return true;
}   

Question 2. I'm not sure how to formulate this into the proper prepared statement which is completely secured. Does $entries and $id both need it?

Thanks

  • 写回答

1条回答 默认 最新

  • douqiangchuai7674 2019-07-09 04:14
    关注

    Answering to your first question: Prepare statement in WordPress prepares a SQL query for safe execution. It uses sprintf()-like syntax.

    For your second question: In your case of get_var query the statement should be something like this:

    global $wpdb;
    $entries = $wpdb->prefix . 'simpledir_entries';
    $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM %s WHERE rel = %d;", $entries, $id));
    
    if($count <= 0){
        return false;
    }else{
        return true;
    }
    

    Good Luck!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错