如何从SELECT查询中返回找到的行数
-
-
表的名称和表前缀是什么?What is the name of the table and the table prefix?
- 0
- 2014-01-29
- Chittaranjan
-
@Chittaranjan表名称是wp_postviews_ips,不过我不确定您由表前缀表示的含义.@Chittaranjan Table name is wp_postviews_ips, I'm not sure what you mean by table prefix though.
- 0
- 2014-01-29
- Swen
-
从$ wpdb-> wp_postviews_ips中删除" $ wpdb->"似乎可以解决问题!Removing "$wpdb->" from $wpdb->wp_postviews_ips seemed to do the trick!
- 0
- 2014-01-29
- Swen
-
这就是我要求输入表名和前缀的原因.所有wordpress表都有一个前缀,您可以在设置wordpress网站时设置该前缀.以下是有关[codex]的更多详细信息(http://codex.wordpress.org/Creating_Tables_with_Plugins#Database_Table_Prefix)请使用正确的表名检查我更新的答案.That's the reason I had asked for the table name and prefix. All wordpress tables have a prefix which you set during setting up the wordpress site. Here are more details on [codex](http://codex.wordpress.org/Creating_Tables_with_Plugins#Database_Table_Prefix) Please check my updated answer with correct use of table name.
- 0
- 2014-01-29
- Chittaranjan
-
2 个回答
- 投票数
-
- 2014-01-29
如果您只是想获取计数,则在您的sql中使用
$wpdb->get_var();
以及使用COUNT()
会更好:/p>### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $rowcount; } postviews_get_ip($id, $_SERVER['REMOTE_ADDR']); //both $id and $_SERVER['REMOTE_ADDR']) return the values I'm searching for in the database
关于上一个示例中出了什么问题,您没有将
$wpdb->get_results()
实例分配给变量,并且没有它,则没有$wpdb->num_rows;
只会返回零,因为它实际上不是从查询实例中拉出,而是全局的$ wbdb对象.如果您确实想使用
get_results()
:### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $ipquery= $wpdb->get_results("SELECT * FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); $rowcount = $ipquery->num_rows; return $rowcount; } postviews_get_ip($id, $_SERVER['REMOTE_ADDR']); //both $id and $_SERVER['REMOTE_ADDR']) return the values I'm searching for in the database
但是除非您需要结果,否则我不会认为需要这样做,在这种情况下,我只返回
$ipquery
对象并在其上使用num_rows
我需要它:### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $ipquery = $wpdb->get_results("SELECT * FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $ipquery; } $someVariable = postviews_get_ip($id, $_SERVER['REMOTE_ADDR']); //both $id and $_SERVER['REMOTE_ADDR']) return the values I'm searching for in the database echo $someVariable->num_rows;
If you are merely trying to get a count,
$wpdb->get_var();
along with usingCOUNT()
in your sql will be better:### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $rowcount; } postviews_get_ip($id, $_SERVER['REMOTE_ADDR']); //both $id and $_SERVER['REMOTE_ADDR']) return the values I'm searching for in the database
As to what went wrong in your previous example, you weren't assigning your
$wpdb->get_results()
instance to a variable, and without it$wpdb->num_rows;
is just going to return zero as it isn't actually pulling from the instance of the query, but rather the global $wbdb object.If you do want to use
get_results()
:### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $ipquery= $wpdb->get_results("SELECT * FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); $rowcount = $ipquery->num_rows; return $rowcount; } postviews_get_ip($id, $_SERVER['REMOTE_ADDR']); //both $id and $_SERVER['REMOTE_ADDR']) return the values I'm searching for in the database
But I wouldn't see the need for that unless you needed the results, in which case I would just return the
$ipquery
object and usenum_rows
on it when I needed it:### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $ipquery = $wpdb->get_results("SELECT * FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $ipquery; } $someVariable = postviews_get_ip($id, $_SERVER['REMOTE_ADDR']); //both $id and $_SERVER['REMOTE_ADDR']) return the values I'm searching for in the database echo $someVariable->num_rows;
-
小加成.执行任何查询以防止SQL注入时,应始终使用prepare(https://developer.wordpress.org/reference/classes/wpdb/prepare/).Small addition. You should always use prepare (https://developer.wordpress.org/reference/classes/wpdb/prepare/) when executing any queries to prevent sql injection.
- 1
- 2019-05-29
- Maciej Paprocki
-
实际上,这不应该是一个很小的补充,这很重要,不要使您的代码可通过sql注入进行利用.Actually that shouldnt be a small addition, that's very important not to make your code exploitable via sql injection.
- 0
- 2019-09-12
- Max Carroll
-
- 2014-01-29
似乎查询是错误的.
$ip
是字符串,因此您应该在下面加上单引号$wpdb->get_results("SELECT * FROM {$wpdb->prefix}postviews_ips WHERE postid = $id AND ip = '$ip'");
Seems the query is wrong.
$ip
is string so you should put single quote around that as below$wpdb->get_results("SELECT * FROM {$wpdb->prefix}postviews_ips WHERE postid = $id AND ip = '$ip'");
-
尝试过此方法,但仍返回0.Tried this and it still returns 0.
- 0
- 2014-01-29
- Swen
我编写了一个函数,该函数应该返回在SELECT查询中找到的行数,但它似乎总是返回0或数组.我已经搞混了大约一个小时了,但我仍然无法弄清楚!我确定我做错了什么.
MySQL表
PHP