6

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:

  1. CI 2.x + (done)
  2. Smarty 3 (done)
  3. PhpUnit (not yet)
  4. PDO (not yet)

so if you have also any hints for integrate phpunit feels free to answer. Ty in advance

th3n3rd
  • 375
  • 1
  • 3
  • 20

2 Answers2

8

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

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
jmadsen
  • 3,635
  • 2
  • 33
  • 49
  • 2
    Lone link is considered a poor answer (see [faq#deletion]) since it is meaningless by itself and **target resource is not guaranteed to be alive in the future**. [It would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – j0k Mar 16 '13 at 08:08
8

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.

birderic
  • 3,745
  • 1
  • 23
  • 36
  • really ty for the phpunit hint!!!! you've helped me a lot, i've lost hours on looking for smth for phpunit but i only found old ci libraries etc...about pdo u right, i dunno why but at home its works fine, but at works neighter ->query or AR seems to work, i should investigate deeply. However ty again XD – th3n3rd Jan 05 '12 at 18:09
  • 1
    i used xdebug to trace what CI does when i call both active records or query method, and in both situation CI doesnt use pdo "prepare" statement lol ... what the advantages of using pdo driver if CI dont' use pdo function inside!!! I want to use pdo in particular because it has a query binding method (not like codeigniter one). – th3n3rd Jan 09 '12 at 10:06
  • This is super old but make sure to prepare the query. http://ellislab.com/forums/viewthread/218455/ – RaGe10940 Feb 12 '13 at 12:56
  • Have there been problems with PDO previously? (such as in Active Records and all) Does this have any specific version of CI that this would work since? – Romeo Sierra Aug 22 '18 at 12:20