I trying to retrieve data from DB...But I face some problem
Following is my coding:
In Controller :
public function actionFinalCheck()
{
$gametitle=GamesDevelopersApp::model()->find('develper_id=1',array('gametitle'));
$this->render('finalcheck',array('gametitle'=>$gametitle));
}
In View(PHP):
<?php print_r($gametitle); ?>
What I need is "select gametitle from db where developer_id=1"
but in Yii I not sure how to do
OR any better way to retrieve data from DB and display in view ? Thanks
You are passing the parameter :developer_id but are not using it in the condition. Try passing it a CDBCriteria object like below:
$criteria = new CDbCriteria();
$criteria->compare('id', 1); // Check that the column gametitle is equal to 1
$gametitle=GamesDevelopersApp::model()->findAll($criteria);
See findAll method for ACtiveRecord http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail for details
I use this it worked fine for me !
In controller
public function actionFinalCheck()
{
$developer_id = 'developer_id=1';
$gametitle=GamesDevelopersApp::model()->find($developer_id);
$this->render('finalcheck',array('gametitle'=>$gametitle));
}
In View(PHP):
<?php echo CHtml::encode($gametitle->gametitle); ?>
You can do that by the ways listed below:
Active Record
You need to have a model and by model you can fetch data like below:
$object=GamesDevelopersApp::model()->findByAttributes(array("develper_id"=>1));
echo $object->gametitle; // prints gametitle
Query Builder
$row=Yii::app()->db->createCommand()->select('gametitle')->from('db')->where('id=1')->queryRow();
echo $row['gametitle']; //prints gametitle
DAO (Data Access Objects)
$sql="select gametitle from db where developer_id=1";
$command=Yii::app()->db->createCommand($sql)->queryRow();
echo $command['gametitle']; //prints gametitle
public function getQuotes()
{
$sql="select * from db";
$command=Yii::app()->db->createCommand($sql)->queryAll();
foreach($command as $commands)
echo $commands['gametitle'];
}
In view:
<?php echo CHtml::encode($model->gametitle)?>