Using mysql database gives $dns undifined

I’m trying to make mysql work without luck.

Although I’ve set the mysql parameters in main.php, when i’m trying to make a connection I’m getting Undefined variables for $dns,$password and $user.

Specifically I’m doing this in my index.php file under protected/views/site off the default webapp creation.

$connection=new CDbConnection($dsn,$username,$password);


And I’m getting this error

PHP notice

Undefined variable: password


01 <?php $this->pageTitle=Yii::app()->name;

02 $connection=new CDbConnection($dsn,$username,$password);

03 $connection->active=true;

04 ?>

The mysql setting in main.php config file

// application components



			// enable cookie-based authentication



		// uncomment the following to enable URLs in path-format












			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',


		// uncomment the following to use a MySQL database




			'connectionString' => 'mysql:host=localhost;dbname=testing',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '',

			'charset' => 'utf8',




			// use 'site/error' action to display errors



I don’t have a password on local machine.

I’m new on Yii and I’m probably missing something here.

Thanks in advance.

Obviously you didn’t assign to the local $password variable. So why don’t you use the default ‘db’ application component (of class CDbConnection) that Yii instantiates for you?

I recommend that you follow the steps in this section of the guide before trying out variations.