gpt4 book ai didi

php - WordPress 如何从 get_results 函数获取值

转载 作者:行者123 更新时间:2023-11-29 10:51:31 26 4
gpt4 key购买 nike

我尝试通过 WordPress get_results 函数从返回对象获取值,但没有成功

示例代码

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

echo $myrows->dviews; // echo nothing

所以知道如何从中获取整数作为dviews返回。

最佳答案

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.

对于单个记录

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

对于多个记录

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

请注意:如果您有表前缀,请使用 $wpdb->prefix 获取正确的表名称。

希望这有帮助!

关于php - WordPress 如何从 get_results 函数获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43655796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com