How To Call Data From Database?

I am using php::pdo as my database table locator. I wanted to show the content of a table from my database but I dont know the right way to get it/ declare.

Here is what I’ve done in my viewuserpost.php

<?php

$uid = Yii::app()->session[‘iduser’];

echo $uid;

?>

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

    'dataProvider' =&gt; new CArrayDataProvider(&#036;results),


    'columns' =&gt; array( 'Title', 'Content'


		array(                               //I think my problem starts here


			'name' =&gt; 'title',


			'type' =&gt; 'raw',


			'value' =&gt; 'CHtml::encode(&#036;data-&gt;title)'


			);


     //specify the colums you wanted here


    ),


));

?>

here is what I’ve done in my Model ContentForm.php

public function getTitle() {

&#036;uid = Yii::app()-&gt;session['iduser'];





&#036;db = new PDO('mysql:host=localhost; dbname=yiimysql; charset=utf8', 'joseph', 'pcG559Jh');


&#036;sql = &quot;SELECT title FROM tbl_content where userid = '&#036;uid' &quot;;





foreach (&#036;db-&gt;query(&#036;sql) as &#036;row) {


	&#036;this-&gt;title = &#036;row['title'];


	array_push(&#036;this-&gt;results,&#036;row['title']);


} 





}


public function getResults() {


		return &#036;this-&gt;results;


}





public function getUserPost(){





&#036;results = array();


&#036;db = new PDO('mysql:host=localhost; dbname=yiimysql; charset=utf8', 'joseph', 'pcG559Jh');


&#036;db -&gt; setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


&#036;db -&gt; setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


&#036;sql = 'SELECT title FROM tbl_content ORDER BY title';











}

here is my controller SiteController.php

public function actionViewUserPost()


{


	&#036;results = array();


	&#036;model=new ContentForm('getUserPost');


	&#036;db = new PDO('mysql:host=localhost; dbname=yiimysql; charset=utf8', '*****', '******');


	&#036;db -&gt; setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


	&#036;db -&gt; setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


	&#036;sql = 'SELECT title FROM tbl_content WHERE userid = 1';


			


	&#036;this-&gt;render('viewuserpost',array('results'=&gt;&#036;results));


}

When using Yii you better use all the db features of the framework.

  • DAO

  • Query Builder

  • Active record