[英]WPDB: count number of times a particular value appears
I am trying to count the number of times the value of "yes" appears in the column "helpful" in the table "wp_wthp_helpful_log" for the current post in the loop. 我正在尝试计算循环中当前帖子的表“ wp_wthp_helpful_log”中“帮助”列中“是”值出现的次数。
Here is what I have so far, but isn't working. 这是我到目前为止的内容,但是没有用。 Any guidance would be helpful. 任何指导都会有所帮助。 This is the first time I have ever tried to use wpdb. 这是我第一次尝试使用wpdb。
<?php
global $wpdb;
$helpfulcount = $wpdb->get_var(" SELECT COUNT(*) FROM {$wpdb->wp_wthp_helpful_log} WHERE post_id = $id AND helpful = 'yes' ");
if ( $helpfulcount > 0 ) {
echo 'I got a count of '.$helpfulcount ;} else { };
?>
Got it working with this... 可以使用它...
<?php
global $wpdb;
global $post;
$postid = $post->ID;
$helpfulcount = $wpdb->get_var(" SELECT COUNT(*) FROM wp_wthp_helpful_log WHERE post_id = $postid AND helpful = 'yes' ");
if ( $helpfulcount > 0 ) {
echo 'I got a count of '.$helpfulcount ; } else { };
?>
You have to put yes
in quotes because it's a literal string, not a name that MySQL recognizes: 您必须在引号中加上yes
,因为它是文字字符串,而不是MySQL可以识别的名称:
<?php
$helpfulcount = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->wp_wthp_helpful_log} WHERE postid = $id AND helpful = 'yes'");
if ( $helpfulcount > 0 ) {
echo 'I got a count of '.$helpfulcount ; } else { };
?>
Note also the {
and }
around $wpdb->wp_wthp_helpful_log
. 注意$wpdb->wp_wthp_helpful_log
周围的{
和}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.