Database Connection Error

Hey guys,

It’s been a while since I am searching for a solution to this error…Please help me if you have any Idea.

I am currently trying unit testing with Yii…I’ve installed PHPUnit and Pear successfully and can conduct Unit Tests using PhpUnit seperately.

Now…Using Yii when I test a database Connection…It gives an error.

Here’s my DbTest.php

<?php

class DbTest extends CTestCase

{

public function testConnection()

{

$this->assertNotNull(Yii::app()->db->connectionString);

}

}

and Now When I use the Command Line to get the phpunit working,It gives an error.

C:\wamp\www\Happyfriendship\trackstar\protected\tests\unit\DbTest.php:6

FAILURES!

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

C:\wamp\www\Happyfriendship\trackstar\protected\tests>phpunit .\unit\DbTest.php

PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from C:\wamp\www\Happyfriendship\trackstar\protected\tests\ph

punit.xml

E

Time: 48 ms, Memory: 2.75Mb

There was 1 error:

  1. DbTest::testConnection

CDbException: CDbConnection failed to open the DB connection.

C:\wamp\framework\db\CDbConnection.php:388

C:\wamp\framework\db\CDbConnection.php:330

C:\wamp\framework\db\CDbConnection.php:308

C:\wamp\framework\base\CModule.php:387

C:\wamp\framework\base\CModule.php:103

C:\wamp\www\Happyfriendship\trackstar\protected\tests\unit\DbTest.php:6

FAILURES!

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

Now When I test the database connection manually using

>> echo Yii::app()->db->connectionString;

It again gives an error like CDbConnection failed to open the DB connection: could not find driver.

P.S

I would like to tell that I am able to generate CRUD model and can add,delete and manage using the same database credentials…So,I guess the credentials are right…I don’t know what to do.

Console and test apps use separate configs, see protected/config folder.

I suppose that’s why your web app works and test app doesn’t.

Hi Orey,

I had already configured test.php , main.php and console.php ( Enabling Mysql and driving the same credentials.)

Looking at various aspects …

I have also enable extensions like php_pdo_mysql.dll

Still…Same error.

Sorry, I haven’t got better ideas.

If your webapp is working, then DB is fine, PDO is fine, all is fine.

If I were you, I’d see what’s in bootstrap file.

Maybe you’ve changed some folders and now test suite cannot reach your config.

First using the $: yiic shell .\config\main.php

Here’s the complete output

C:\wamp\www\Happyfriendship\trackstar\protected>yiic shell .\config\main.php

Yii Interactive Tool v1.1 (based on Yii v1.1.14)

Please type ‘help’ for help. Type ‘exit’ to quit.

>> echo Yii::app()->db->connectionString;

exception ‘CDbException’ with message ‘CDbConnection failed to open the DB connection: could not find driver’ in C:\wamp\framework\db\CDbConnection.php:382

Stack trace:

#0 C:\wamp\framework\db\CDbConnection.php(330): CDbConnection->open()

#1 C:\wamp\framework\db\CDbConnection.php(308): CDbConnection->setActive(true)

#2 C:\wamp\framework\base\CModule.php(387): CDbConnection->init()

#3 C:\wamp\framework\base\CModule.php(103): CModule->getComponent(‘db’)

#4 C:\wamp\framework\cli\commands\ShellCommand.php(131) : eval()'d code(1): CModule->__get(‘db’)

#5 C:\wamp\framework\cli\commands\ShellCommand.php(131): eval()

#6 C:\wamp\framework\cli\commands\ShellCommand.php(99): ShellCommand->runShell()

#7 C:\wamp\framework\console\CConsoleCommandRunner.php(71): ShellCommand->run(Array)

#8 C:\wamp\framework\console\CConsoleApplication.php(92): CConsoleCommandRunner->run(Array)

#9 C:\wamp\framework\base\CApplication.php(180): CConsoleApplication->processRequest()

#10 C:\wamp\framework\yiic.php(33): CApplication->run()

#11 C:\wamp\www\Happyfriendship\trackstar\protected\yiic.php(7): require_once(‘C:\wamp\framewo…’)

#12 {main}

I guess something is stopping CDbConnection class to instantiate an object which gives the desired output which should have been something like

mysql:host=localhost;dbname=trackstar

Moreover…This error is not making me use the migration tool…

C:\wamp\www\Happyfriendship\trackstar\protected>yiic migrate

Yii Migration Tool v1.0 (based on Yii v1.1.14)

exception ‘CDbException’ with message ‘CDbConnection failed to open the DB connection: could not find driver’ in C:\wamp\framework\db\CDbConnection.php:382

Stack trace:

#0 C:\wamp\framework\db\CDbConnection.php(330): CDbConnection->open()

#1 C:\wamp\framework\db\CDbConnection.php(308): CDbConnection->setActive(true)

#2 C:\wamp\framework\base\CModule.php(387): CDbConnection->init()

#3 C:\wamp\framework\cli\commands\MigrateCommand.php(442): CModule->getComponent(‘db’)

#4 C:\wamp\framework\cli\commands\MigrateCommand.php(451): MigrateCommand->getDbConnection()

#5 C:\wamp\framework\cli\commands\MigrateCommand.php(482): MigrateCommand->getMigrationHistory(-1)

#6 C:\wamp\framework\cli\commands\MigrateCommand.php(84): MigrateCommand->getNewMigrations()

#7 [internal function]: MigrateCommand->actionUp(Array)

#8 C:\wamp\framework\console\CConsoleCommand.php(172): ReflectionMethod->invokeArgs(Object(MigrateCommand), Array)

#9 C:\wamp\framework\console\CConsoleCommandRunner.php(71): CConsoleCommand->run(Array)

#10 C:\wamp\framework\console\CConsoleApplication.php(92): CConsoleCommandRunner->run(Array)

#11 C:\wamp\framework\base\CApplication.php(180): CConsoleApplication->processRequest()

#12 C:\wamp\framework\yiic.php(33): CApplication->run()

#13 C:\wamp\www\Happyfriendship\trackstar\protected\yiic.php(7): require_once(‘C:\wamp\framewo…’)

#14 {main}

WHAT TO DO ?? :o

Php can be loading another ini file for CLI, so don’t forget to check it also.

You can find it out quickly by running phpinfo() from CLI and examining the output. running “php --ini” (I’m not sure the syntax is correct for windows)

If so, either edit this file to enable pdo, or make php CLI to load the ‘good’ one.

(see CLI help for options)

I guess it’s definitely the issue of loading a different .ini file when using the CLI…

I just saw the phpinfo()

Configuration File (php.ini) Path C:\Windows

Loaded Configuration File C:\wamp\bin\apache\apache2.4.4\bin\php.ini

I guess it’s loading the .ini file from C:\Windows … Any clue how to get that. ?

I guess it’s definitely the issue of loading a different .ini file when using the CLI…

I just saw the php --ini

Configuration File (php.ini) Path: C:\Windows

Loaded Configuration File: (none)

Scan for additional .ini files in: (none)

Additional .ini files parsed: (none)

It is not loading any configuration File…I guess specifying it would give the solution. !!

Any clue ?

  1. run ‘php --ini’ from the command line

  2. see the value for ‘loaded configuration file’

  3. open this file and check the PDO options.

Wow. Never saw php running w/o config file at all.

Sorry, I’m not a windows guy, so here are the docs for ini on windows:

http://www.php.net/manual/en/configuration.file.php

Also you can run php with option -c to specify config file location.

Hey…After alot of trials.

What Next I did is … I copied the php.ini file and pasted in the C:\Windows

and then again used the command php --ini

Now I get alot of warnings about the dlls not valid Windows 32 application

C:\Users\google>php --ini

Failed loading c:/wamp/bin/php/php5.4.12/zend_ext/php_xdebug-2.2.3-5.4-vc9-x86_64.dll

PHP Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_gd2.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_gd2.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

PHP Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_mbstring.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_mbstring.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

PHP Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_mysql.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_mysql.dll’- %1 is not a valid Win32 application.

in Unknown on line 0

PHP Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_mysqli.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_mysqli.dll’- %1 is not a valid Win32 application.

in Unknown on line 0

PHP Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_pdo_mysql.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_pdo_mysql.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

PHP Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_pdo_sqlite.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library ‘c:/php/ext/php_pdo_sqlite.dll’ - %1 is not a valid Win32 application.

in Unknown on line 0

Configuration File (php.ini) Path: C:\Windows

Loaded Configuration File: C:\Windows\php.ini

Scan for additional .ini files in: (none)

Additional .ini files parsed: (none)

Now it means that The windows is able to load the configuration file but is not able to load the dlls.

Still no solution.

Let me strike my head a little more.

That’s because they’re somewhere in c:/wamp/bin/php/php5.4.12/ext.

So I think the best option for you is to remove copypasted file and set path to working .ini (C:\wamp\bin\apache\apache2.4.4\bin\php.ini ??) using registry or some other way described in docs for ini file location (link above)

I had edited the php.ini file before pasting it into the Windows folder to :

extension_dir = "c:\php\ext"

And then pasted all the extensions in the php folder of c: DRIVE.

BUT THE point to be noted here is that windows is unable to load the .dll files not because they are unavailable but because they are not valid Windows 32 application.

So,I guess I should reinstall the 32 bit wamp server on my 64 bit system.I don’t find any other feasible way.

Checking the documentation,I am not able to clearly pacify the problem.

Do tell me if you find a more appropriate way of setting dynamic path to the commands of php…There’s no appropriate solution available on the internet. !

Hey…

After 15 hours straight I found a solution to live with.

I had to make the CLI search for a configuration file…So what I did was I copied the Content of PHP5.4.12 WHICH contained my php.exe ,php.ini and all the required dlls, and ofcourse PEAR into my protected folder.

Now using the CLI.

C:\wamp\www\Happyfriendship\trackstar\protected>yiic shell .\config\main.php

Yii Interactive Tool v1.1 (based on Yii v1.1.14)

Please type ‘help’ for help. Type ‘exit’ to quit.

>> echo Yii::app()->db->connectionString;

mysql:host=localhost;dbname=trackstar

Also being able to use the migrate.

C:\wamp\www\Happyfriendship\trackstar\protected>yiic migrate

Yii Migration Tool v1.0 (based on Yii v1.1.14)

No new migration found. Your system is up-to-date.

But the same problem exists when I am using unit testing.

Doing the same thing,I copied the content of PHP5.4.12 Into my tests folder and then finding the output.

Same errors occur

C:\wamp\www\Happyfriendship\trackstar\protected\tests>phpunit .\unit\DbTest.php

PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from C:\wamp\www\Happyfriendship\trackstar\protected\tests\ph

punit.xml

E

Time: 52 ms, Memory: 2.75Mb

There was 1 error:

  1. DbTest::testConnection

CDbException: no yii_debug CDbConnection failed to open the DB connection.

C:\wamp\framework\db\CDbConnection.php:388

C:\wamp\framework\db\CDbConnection.php:330

C:\wamp\framework\db\CDbConnection.php:308

C:\wamp\framework\base\CModule.php:387

C:\wamp\framework\base\CModule.php:103

C:\wamp\www\Happyfriendship\trackstar\protected\tests\unit\DbTest.php:6

FAILURES!

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

C:\wamp\www\Happyfriendship\trackstar\protected\tests>

just dump that connection and try to make own PDO object for exception like

new PDO(bla-bla-bla)