我在 Yii 2 中使用查询时遇到此错误。
这是我的 SQL 查询代码:
<?php $fines=Yii::$app->db->createCommand('select sum(fines) as fines from lib_chekout '); ?>
这是我使用 $fines 变量的代码:
<h4 class="pull-left" style="font-size: 20px"> Total fines Collected Till Today : </h4> <p class="text-center" style="font-size: 20px"> <?= $fines ?> </p>
现在的问题是,它显示错误
Object of class yii\db\Command could not be converted to string
如何解决这个问题?
最佳答案
您需要添加queryScalar()命令之后。 像这样:
<?php
$fines=Yii::$app
->db
->createCommand('select sum(fines) as fines from lib_chekout ')
->queryScalar();
?>
并将此 >?
更改为 ?>
关于mysql - yii\db\Command 类的对象无法转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406486/