Does someone have an example of connecting to .mdb via ODBC from Yii?

Thanks a lot.


The Yii framework uses PDO. As such, anything supported by PDO is supported by Yii. All you should have to change to make it connect to a new datasource is your connect string.

To connect to a .mdb database, you will want to use something like this for your connection string

"odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin"

I’m not sure all the options you will need, but that should get your basic connection working.

I got this information from http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.7, so I don’t know much else about it. If you can get all the options for your .mdb file working properly, though, it should connect to the database without any problems. As far as I know.

I have the same problem.

I’m trying to use odbc, and entered

‘db’=>array(‘connectionString’=>“odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\devl\jewel\jewel_be.mdb;Uid=Admin”),

into config/main.php, but it doesn’t work.

When I try to use the model instruction in the shell, I get an error. More precisely, the createPDOInstance() method returns the error.

I can use the same connectionstring when I use PDO directly, so it seems to be a Yii-issue.

Can anyone enlighten me?


What error do you get? I think at least the PDO object should get generated. But that’s not enough to use AR with ODBC. You’ll also need the according classes for schema handling (framework/db/ar/schema). There’s no implementation for ODBC (yet). Maybe give it a try? ;)


These are the error messages I get when I try the model table command in yiic shell:

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

Stack trace:

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

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

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

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

#4 C:\wamp\yii\framework\cli\commands\shell\ModelCommand.php(260): CApplication->getDb()

#5 C:\wamp\yii\framework\cli\commands\ShellCommand.php(144): ModelCommand->run(Array)

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

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

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

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

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

#11 C:\wamp\yii\framework\yiic(15): require_once(‘C:\wamp\yii\fra…’)

#12 {main}

But OK, since odbc is not yet implemented, I will have to reassess the options.

It is a little early for me in Yii to implement it myself. ;)

I see. :) Take your time, it might not be as hard as you think. You could take the classes framework/db/schemas/mysql as template. But i might be wrong: Didn’t look too deep into that part of the implementation yet.

From your error message i’d say: You are missing the PDO ODBC driver in you PHP installation. They are crucial to work with ODBC at all.

Maybe I’ll try it.

Bu given that is has not yet been done, I fear it won’t be that easy

PDO works fine when I use it in my regular php code.