donglian4464 2017-04-27 02:56
浏览 92
已采纳

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 04: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部