dqm74406 2013-07-27 13:14
浏览 11
已采纳

wordpress计数自定义表中相同的行[重复]

This question already has an answer here:

I have a custom table within my wordpress database. I would like to COUNT the rows in the table that have the same value.

So for example:

IP ADDRESS

0.0.0.0.0

0.0.0.0.1

0.0.0.0.0

The output would then be:

there are 2 x 0.0.0.0.0

there are 1 x 0.0.0.0.1

I can achieve the above fine with a normal sql query, but i am trying to do this via the $wpdb class

Here is my query:

$table =  $wpdb->prefix . 'wplt';

$posts = $wpdb->get_results("SELECT ip_address, COUNT(ip_address) FROM $table GROUP BY ip_address");

foreach ( $posts as $post ) {

    echo $post->ip_address;

    echo $post->COUNT(ip_address);

}

With the above i get the following error:

Fatal error: Call to undefined method stdClass::COUNT()

</div>
  • 写回答

1条回答 默认 最新

  • dongwu8064 2013-07-27 14:04
    关注

    This error you are getting because $post object doesn't has any count function

    Fatal error: Call to undefined method stdClass::COUNT()
    

    If you want to show the count of ip address from query you can assign an alias to that count in the query like

    $table =  $wpdb->prefix . 'wplt';
    
    $posts = $wpdb->get_results("SELECT ip_address, COUNT(ip_address) AS ip_count FROM $table GROUP BY ip_address");
    
    foreach ( $posts as $post ) {
    
        echo $post->ip_address;
    
        echo $post->ip_count ;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答