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

时间:2016-12-31 07:46:40

标签: mysql yii2

在Yii 2中使用查询时出现此错误。

这是我的SQL查询代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Client</title>
<link rel="stylesheet" href="../bootstrap-3.3.7/docs/dist/css/bootstrap.min.css">
<script src="../bootstrap-3.3.7/docs/assets/js/vendor/jquery.min.js"></script>
<script src="../bootstrap-3.3.7/docs/dist/js/bootstrap.min.js"></script>
</head>
<body>
<form method="post" action="client.php">
<p><h4><label>Type Your Message Here:<input name = "message" size = "25" maxlength = "30" required></label></h4></p>
<input type="submit" name="sendmsg" class="btn btn-primary" value="send message"/>
</form>
<?php
ob_implicit_flush(true);
$user="abc";
if(empty($_POST)){

}
elseif(isset($_POST['sendmsg'])) {
$message =$_POST["message"];


$host    = "127.0.0.1";
$port    = 25011;

echo "Message To server :".$message;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");

$result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  

socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");

while(true){
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server  :".$result;
ob_flush();
if($result == "start"){
echo "Quiz Started";
ob_flush();
break;
}
}


socket_close($socket);


}


?>

</body>

</html>

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

<?php
session_start();
$_SESSION['pass_start_quiz'] = "yes";
?>

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

  

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

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

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

并将此>?更改为?>