codeigniter 3.1 PDO mysql Error Number: 3D000/1046 No database selected

thep picture thep · Sep 29, 2015 · Viewed 8k times · Source

I had issue with upgrading codeignier 2.xx to 3.xx before and I can not found any completed answer for me. So I'd like to share how I solved it.

In Codeignier official web site show how to connect PDO with Mysql as below

For the PDO driver, you should use the $config[‘dsn’] setting instead of ‘hostname’ and ‘database’:

$config[‘dsn’] = ‘mysql:host=localhost;dbname=mydatabase’

https://www.codeigniter.com/user_guide/database/connecting.html

But I still could not make it work as it was showing error

Invalid or non-existent PDO subdriver

Some site shown how to fixe this by add this to hostname

$db ['default'] ['hostname'] = 'mysql:host=localhost';

But I still got error

Error Number: 3D000/1046

No database selected

Answer

thep picture thep · Sep 29, 2015

I spend time to debug a while I found that once dbdriver is selected to 'pdo' Codeigniter check dsn detail from $db ['default'] ['hostname'] not in $db ['default'] ['dns'] including database name like below.

$db ['default'] ['hostname'] = 'mysql:host=localhost;dbname=my_database';

I hope this will help for anyone may got error like mine.