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

$connection->active=true;



And I’m getting this error




PHP notice


Undefined variable: password


C:\xampp\htdocs\testing\protected\views\site\index.php(1)


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

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

		),

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

		

		'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),

		/*

		'db'=>array(

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

		),*/

		// uncomment the following to use a MySQL database

		

		'db'=>array(

                        'class'=>'CDbConnection',

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

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '',

			'charset' => 'utf8',

		),

		

		'errorHandler'=>array(

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

            'errorAction'=>'site/error',

        ),



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.

/Tommy