【问题标题】:Counting results of a call to the database统计调用数据库的结果
【发布时间】:2014-06-17 13:16:50
【问题描述】:

我已经在 phpmyadmin 中运行了这个:

SELECT user_id, DATE(comment_date) comment_date, 
       COUNT(*) comment_ID 
FROM wp_comments  
WHERE user_id='3' 
GROUP BY user_id, DATE(comment_date) ;

并向我展示了 4 个结果,但是当 PHP 中的这个其他 sim 示例时,示例 3 个结果:

global $current_user, $wpdb;
$count = $wpdb->get_var("SELECT user_id, DATE(comment_date) comment_date, 
                         COUNT(*) comment_ID FROM ".$wpdb->comments.
                       " WHERE user_id='".$current_user->ID."'
                       GROUP BY user_id, DATE(comment_date) ;");
echo $count;

在 PHP 中运行查询时:

echo "SELECT user_id, DATE(comment_date) comment_date, COUNT(*) comment_ID
FROM ".$wpdb->comments." 
WHERE user_id='".$current_user->ID."'
GROUP BY user_id, DATE(comment_date) ;";

显示:

SELECT user_id, DATE(comment_date) comment_date, COUNT(*) comment_ID FROM wp_cmets WHERE user_id='3' GROUP BY user_id, DATE(comment_date) ;

【问题讨论】:

  • echo $current_user->id 并验证查询是否使用您认为的查询
  • 我实际上会回显整个 SQL 命令,以确保您的 cmets 变量也是正确的。
  • 是的,$current_user->ID 是 3,$wpdb->cmets 是 wp_cmets。我检查过...我什至尝试过与另一个用户一起使用,但没有给出预期的结果。
  • 你的意思是先显示4行再显示3行还是先显示4列再显示3列???

标签: php mysql wordpress


【解决方案1】:

所以你确定,userId 是 3?

更新:

$count = $wpdb->get_var()

返回 sql 查询的第一个参数。这是您查询中的 user_id 为 3。

你必须使用

$result = $wpdb->get_results()

那么应该有一个结果数组。

【讨论】:

  • 是的,$current_user->ID 是 3,$wpdb->cmets 是 wp_cmets。我检查过...我什至尝试过与另一个用户一起使用,但没有给出预期的结果。
  • 啊,我收到了错误。 $wpdb->get_var() 返回 sql 查询的第一个参数。这是您查询中的 user_id 为 3。您必须使用 $wpdb->get_results() 这将返回一个数组。
猜你喜欢
  • 2013-01-13
  • 2017-10-20
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
相关资源
最近更新 更多