Chapter 4, DbTest failed!

Hi, all

I am new to this framework, I have been following this book to get me started. However, I have run into this problem in Chapter 4 when I tried to test my database connection.

Output from the console:




There was 1 error:


1) DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.


/Applications/XAMPP/xamppfiles/htdocs/yii/framework/db/CDbConnection.php:388

/Applications/XAMPP/xamppfiles/htdocs/yii/framework/db/CDbConnection.php:331

/Applications/XAMPP/xamppfiles/htdocs/yii/framework/db/CDbConnection.php:309

/Applications/XAMPP/xamppfiles/htdocs/yii/framework/base/CModule.php:388

/Applications/XAMPP/xamppfiles/htdocs/yii/framework/base/CModule.php:104

/Applications/XAMPP/xamppfiles/htdocs/trackstar/protected/tests/unit/DbTest.php:5

/usr/bin/phpunit:46


FAILURES!

Tests: 1, Assertions: 0, Errors: 1.



I strictly followed the steps in the book, and I tried both main.php and test.php, they both did not work as expected. Can anyone help me? I am driving crazy…

Note: Even if I use the default sqllite configuration, when I changed the name of the .db file, it still passed the test! What on earth is going on here???

can you post the entire main.php in config folder?

Hi,

I have also been struggling with this same problem for a while now.

I’ve read through similar post: CDbException: CDbConnection failed to open the DB connection (both pages) and still am no closer to the solution.

Any help with this matter would be greatly appreciated!

The main.php file is as follows:





<?php


// uncomment the following to define a path alias

// Yii::setPathOfAlias('local','path/to/local-folder');


// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'TrackStar',


	// preloading 'log' component

	'preload'=>array('log'),


	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),


	'modules'=>array(

		// uncomment the following to enable the Gii tool

		/*

		'gii'=>array(

			'class'=>'system.gii.GiiModule',

			'password'=>'Enter Your Password Here',

		 	// If removed, Gii defaults to localhost only. Edit carefully to taste.

			'ipFilters'=>array('127.0.0.1','::1'),

		),

		*/

	),


	// 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/trackstar_dev.db',

		),

                

		*/


		

		'db'=>array(

			'connectionString' => 'mysql:host=localhost;port=3306;dbname=trackstar_dev',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => 'root',

			'charset' => 'utf8',

		),

		

		'errorHandler'=>array(

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

            'errorAction'=>'site/error',

        ),

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),

				// uncomment the following to show log messages on web pages

				/*

				array(

					'class'=>'CWebLogRoute',

				),

				*/

			),

		),

	),


	// application-level parameters that can be accessed

	// using Yii::app()->params['paramName']

	'params'=>array(

		// this is used in contact page

		'adminEmail'=>'webmaster@example.com',

	),

);




Replying to my own post as today I found the solution as posted by (Alan Cheung - post 17) at CDbException: CDbConnection failed to open the DB connection resolved the problem.

Here is the code:




'db'=>array(

          'connectionString'=>'mysql:host=localhost;dbname=trackstar_dev;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',

          'username'=>'root',

          'password'=>'root',

          'emulatePrepare'=>true

),



Thanks Alan!!