PHP显示列值的总和结果

时间:2017-12-15 03:46:01

标签: php mysql wordpress

我正在尝试在我的wordpress网站的wp_users表中显示列名Array的总和结果。为此,我编写了以下代码,但它显示了<?php $result=$wpdb->get_results("SELECT sum(ref_com) as result FROM ".$wpdb->prefix."users WHERE referredby = '$username'"); echo $result; ?> 文本作为输出值。我在做什么错?以下是我的代码

class LoggedInSetup(TestCase):

    def setUp(self):
        self.client = Client()
        self.user = User.objects.create(username="lolwutboi", password="whatisthepassword")
        self.client.login(username='lolwutboi', password='whatisthepassword')

        number_of_submissions = 13
        for sub in range(number_of_submissions):
            Submission.objects.create(title='Amazing', description='This is the body of the post',
                                      author=str(self.user), pub_date=timezone.now())


class LoggedInTestCases(LoggedInSetup):

    def test_logged_in_post_reachable(self):
        self.client.login(username='lolwutboi', password='whatisthepassword')
        resp = self.client.get(reverse('submission_post'))
        self.assertEqual(resp.status_code, 200)

2 个答案:

答案 0 :(得分:2)

默认情况下,get_results函数返回行对象数组。因此,如果您直接执行echo $result,则会打印Array。请尝试改为echo $result[0]->result

答案 1 :(得分:1)

循环遍历数组并回显结果

foreach($result as $r){

 echo $r->result;

}