最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - problem with sql query

programmeradmin1浏览0评论

I have a problem with quotes with query.

My code:

   $this->table_name= 'table_name';

    $how_much = $this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(*) FROM {$this->table_name}"));

I have nothing notice or error and site break down.

EDIT

Probably $wpdb->prepare need arguments to work.

I have a problem with quotes with query.

My code:

   $this->table_name= 'table_name';

    $how_much = $this->wpdb->get_var($this->wpdb->prepare("SELECT COUNT(*) FROM {$this->table_name}"));

I have nothing notice or error and site break down.

EDIT

Probably $wpdb->prepare need arguments to work.

Share Improve this question edited Jul 21, 2019 at 23:17 Jaron asked Apr 21, 2019 at 16:44 JaronJaron 458 bronze badges 3
  • 3 I think prepare needs 2 arguments, link.... But can I ask you what you need to retrieve? there might be a chance that you don't need to run an SQL query... – Brada Commented Apr 21, 2019 at 17:15
  • I need count all records in table and return result. – Jaron Commented Apr 21, 2019 at 17:21
  • 1 You use the function totally, wrong... '%s' it's just a string, It's for the first argument and it means something like "Replace this %s with a variable that must be of type string.". More of it here. For the second argument, you need to put the actual variable, that needs to be verify to not contain any vulnerability... If I were you... I wouldn't play with the database... If you need to do that, you must know what you are doing, and what you can expect... What table do you need to check for? It is a custom table or it's an WP one? – Brada Commented Apr 21, 2019 at 17:35
Add a comment  | 

1 Answer 1

Reset to default 1

Your code should look something like this:

 $this->table_name= 'table_name';

    $how_much = $this->wpdb->get_var("SELECT COUNT(*) FROM $this->table_name");

We don't need to use the prepare method, since this is not meant for table names, more here

And also... as you what are trying to do is going not in a good direction... I don't what to criticize you or anything... But I really suggest you to ask someone who knows better what WordPress can do, about what you need to do, and maybe he will suggest a better approach.

P.S: When you develop something, is good to enable the error/warnings/notices, and have a debugger, to know what's going on.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论