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 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺