donglian4464 2017-04-27 10:56
浏览 91
已采纳

Wordpress如何从get_results函数中获取值

I'm trying to get value from return object by WordPress get_results function but no success with it

Sample code

$myrows = $wpdb->get_results("SELECT SUM(view) as dviews FROM view_count WHERE post_id = '$pid'");

echo $myrows->dviews;   // echo nothing 

so any idea how to get the integer return as dviews from it.

  • 写回答

4条回答 默认 最新

  • duancan65665 2017-04-27 12:02
    关注

    First of all if you want to get only one record then use get_row() instead of get_results() because it will fetch all the data retrieved by SQL query.

    For single record

    $result = $wpdb->get_row("SELECT SUM(view) as dviews FROM view_count WHERE post_id = '$pid'");
    echo $result->dviews;
    

    For multiple records

    $results = $wpdb->get_results("SELECT SUM(view) as dviews FROM view_count WHERE post_id = '$pid'");
    foreach ($results as $result)
    {
        echo $result->dviews;
    }
    

    Please Note: If you have a table prefix the use $wpdb->prefix to get the correct table name.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?