在前端Wordpress站点上显示SQL查询结果
我正在尝试获取 Wordpress
数据库中的列中的总数据 (SUM)。我使用以下 SQL(和 PHP)代码来运行请求;但是,我不知道如何在我的网站前端显示结果。
global $wpdb;
$avg_items_purchased = $wpdb->get_results('SELECT SUM(items_purchased) FROM cogs');
print_r($avg_items_purchased);
或者
var_dump($avg_items_purchased);
我尝试使用 print_r($avg_items_purchased)
;和var_dump($avg_items_purchased)
;但它输出的信息比我想要的更多。在输出中,它打印我正在查找的数据,但我只想显示 items_purchased
的总和。
有没有一种方法可以简单地输出变量,而不需要其他数据?
I am trying to get the total data (SUM) within a column in a Wordpress
database. I am using the following SQL (and PHP) code to run the request; however, I do not know how to display the result on the front end of my site.
global $wpdb;
$avg_items_purchased = $wpdb->get_results('SELECT SUM(items_purchased) FROM cogs');
print_r($avg_items_purchased);
or
var_dump($avg_items_purchased);
I have tried using print_r($avg_items_purchased)
; and var_dump($avg_items_purchased)
; but it outputs more information than I would like. Within the output, it prints the data that I am looking for, but I would like only the SUM of items_purchased
to be displayed.
Is there a way I can simply output the variable, without the other data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 $wpdb->get_var() 的工作。您将从类似这样的内容中获得更清晰的输出:
这将是一个 HTML 段落,内容如下
This is a job for $wpdb->get_var(). You'll get a cleaner output from something like this:
It will be an HTML paragraph saying