Codeigniter PDO integration Codeigniter PDO integration database database

Codeigniter PDO integration


You use PDO the same way you use any other database driver in CodeIgniter. If you are still unsure then I would recommend reading the documentation on the Database Class.

You can issue standard queries by explicitly writing the query or you can use the Active Record Class (which is more of a query builder).

Here are some examples:

// Standard query$results = $this->db->query('SELECT name, title, email FROM my_table');// Active record$this->db->select('name, title, email');$results = $this->db->get('my_table');

As for integrating PHPUnit, have a look at https://github.com/cmillr/CI-PHPUnit (I haven't tested it myself) or look around the CodeIgniter forums. I've seen a ton of topics on integrating PHPUnit with CodeIgniter.


You need to change your config a little:

'dsn'   => 'mysql:host=localhost;dbname=codeigniter',//'hostname' => 'localhost','username' => 'codeigniter','password' => 'codeigniter','database' => 'codeigniter',

Notice we use dsn, not hostname.

After that, simply use your $this->db-> like you always do - the PDO driver will translate everything to PDO methods

A little dated, but the topic is lacking clear explanations & docs so I wrote this - hope it helps clarify for people:

http://codebyjeff.com/blog/2013/03/codeigniter-with-pdo