i did lot of research on the web but i didnt find anything that could help me to use PDO in codeigniter. I saw in the change lof of CI 2.1.0(i think) that pdo driver was added to the framwork. I ended up now with a database.php config file that looks like this:
$db['default']['hostname'] = 'mysql:host=myhostname;port=myport';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydb';
$db['default']['dbdriver'] = 'pdo';
So now(after a lot of wasted time to get the snippet above to work -.- ) i receive no error about connection, but HOW TO EXECUTE QUERY NOW? i cant figure out what syntax will work and how to build queries. Anyone have hints?
PS: if you're wordering about why i need pdo in ci, the answer is my boss want me to create a structured enviroment with:
so if you have also any hints for integrate phpunit feels free to answer. Ty in advance
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With