yii\db\Command 类的对象无法转换为字符串 [英] Object of class yii\db\Command could not be converted to string

查看:46
本文介绍了yii\db\Command 类的对象无法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Yii 2 中使用查询时遇到此错误.

I have got this error while using query in Yii 2.

这是我的 SQL 查询代码:

This is my code for SQL query :

<?php $fines=Yii::$app->db->createCommand('select sum(fines) as fines from lib_chekout '); ?>

这是我使用 $fines 变量的代码:

And this is code where i used that $fines variable :

<h4 class="pull-left" style="font-size: 20px"> Total fines Collected Till Today : </h4> <p class="text-center" style="font-size: 20px"> <?= $fines ?> </p>

现在的问题是,它显示错误

Now the problem is, it shows error

yii\db\Command 类的对象无法转换为字符串

Object of class yii\db\Command could not be converted to string

我该如何解决这个问题?

How can I solve this problem?

推荐答案

您需要添加 queryScalar() 在命令之后.像这样:

You need to add queryScalar() after the command. Like this:

<?php 
$fines=Yii::$app
            ->db
            ->createCommand('select sum(fines) as fines from lib_chekout ')
            ->queryScalar(); 
?>

并将这个 >? 改为 ?>

这篇关于yii\db\Command 类的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆