2

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

2
  • can you try using print_r instead of echo and see what happens? Commented Oct 28, 2014 at 1:56
  • @roullie tried result is "Array()" <-this word Commented Oct 28, 2014 at 1:57

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

and id developer_id is the primary key, use findByPK($developer_id)
Nice One Ali. Banging My Head Since Last 1 Hour And, I Got Here. Thank You.
0

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); ?>

Comments

0

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

3 Comments

thanks but if i have to assign id is 1? Cause normally is select gametitle from db where id='1';
include(CdbCriteria.php): failed to open stream: No such file or directory
Sorry I had an incorrect case on the CDbCriteria name. It's corrected now. Also, if your key is id you will need to use that for the field name. Yii determines which table you are querying from the model
0
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)?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.