php 从 cdbcommand Yii 获取查询结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20016336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get query results from cdbcommand Yii
提问by General Electric
I've been trying to get the results from my query for the past two hours, in my model I have this
在过去的两个小时里,我一直试图从我的查询中获取结果,在我的模型中,我有这个
public function getQuotes()
{
$data = Yii::app()->db->createCommand('Select fromm from city_fare_final');
$data->queryRow();
return $data ;
}
in the controller
在控制器中
public function actionIndex()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$model=new QuoteForm();
if(isset($_POST['QuoteForm']))
{
$model->attributes=$_POST['QuoteForm'];
if ($model->validate())
{
$priceTable=new CityFareFinal;
$priceTable->fromm=$model->pickupL;
$priceTable->too=$model->dropoffL;
$priceTable->type_of_car=$model->type;
this->render('result',array('model'=>$priceTable))
}
}
else
{
$this->render('index',array('model'=>$model));
}
}
and in the view
并且在视图中
<div id="moduleResult">
<span><?php echo $model->getQuotes() ;?><------ Here</span>
</div>
but it always give me an error saying "Object of class CDbCommand could not be converted to string ", what can I do to get the results of my query made in the model???
但它总是给我一个错误,说“CDbCommand 类的对象无法转换为字符串”,我该怎么做才能获得模型中查询的结果???
Regards Gabriel
问候加布里埃尔
回答by secretlm
public function getQuotes()
{
$data = Yii::app()->db->createCommand('Select fromm from city_fare_final');
$data->queryRow();
return $data ;
}
Your getQuotes() return Object of class CDbCommand:
你的 getQuotes() 返回Object of class CDbCommand:
+ You returned $data in the function instead of $data->queryRow().
By the way, you cannot use echofor arraydata.
The below example is used for fetching data from DB to view by using DAO with Yii: I suppose you have Person model and Person controller
顺便说一下,您不能echo用于array数据。下面的示例用于通过使用 DAO 和 Yii 从 DB 获取数据以查看:我想你有 Person 模型和 Person 控制器
In your Person model:
在您的 Person 模型中:
function getData() {
$sql = "SELECT * from Person";
$data = Yii::app()->db
->createCommand($sql)
->queryAll();
return $data;
}
In your controller:
在您的控制器中:
function index(){
$data = Person::model()->getData();
$this->render('your_view',array(
'data'=>$data,
));
}
In your view: you can foreachyour data to echo items in the arraydata:
在您看来:您可以通过foreach您的数据来回显数据中的项目array:
<?php foreach($data as $row): ?>
//show something you want
<?php echo $row->name; ?>
<?php endforeach; ?>
回答by Chetan Ameta
$data->queryRow();returns result in array format. Your code is returning $datawhich is an object not result of query. That's why you are getting this error.
$data->queryRow();以数组格式返回结果。您的代码返回的$data是一个对象,而不是查询的结果。这就是您收到此错误的原因。
If you want to fetch single value you can use $data->queryScalar();
如果你想获取单个值,你可以使用 $data->queryScalar();
In case of queryRow()your code will be
如果queryRow()您的代码将是
public function getQuotes()
{
$data = Yii::app()->db->createCommand('Select * from city_fare_final');
$result = $data->queryRow();
return $result ; //this will return result in array format (single row)
}
for a single field value you code will be
对于单个字段值,您的代码将是
public function getQuotes()
{
$data = Yii::app()->db->createCommand('Select xyz from city_fare_final');
$result = $data->queryScalar();
return $result; //return single value of xyz column
}
I hope this will help.
我希望这将有所帮助。
回答by Vijay
below sample code to traverse rows returned by queryAll
下面的示例代码遍历 queryAll 返回的行
$connection = Yii::app()->db;
$command = $connection->createCommand("Select * from table");
$caterow = $command->queryAll(); //executes the SQL statement and returns the all rows
foreach($caterow as $retcat )
{
echo $retcat["ColumnName"] ;
}
Returns Arrary of rows with fields
返回包含字段的行数组
回答by Ram Pukar
Model: Notices.php:
---------------------------------
public function getNoticesBlog($offset = 0){
$dataResult = Yii::app()->db->createCommand()->select('*')->from($this->tableName())
->andWhere("delete_flg=:delete_flg",array(':delete_flg'=>0))
->andWhere("publish=:publish",array(':publish'=>1))
->limit(3)->offset($offset)->order('created_on DESC')->queryAll();
return $dataResult;
}
Controller: NoticesController.php
控制器:NoticesController.php
$firstNotices = Notices::model()->getNoticesBlog(0);
$secondNotices = Notices::model()->getNoticesBlog(3);
$thirdNotices = Notices::model()->getNoticesBlog(6);
$this->render('Notices',array(
'firstNotices'=>$firstNotices,
'secondNotices'=>$secondNotices,
'thirdNotices'=>$thirdNotices,
)
);

