0

I am trying to get the new PDO driver running in Code Igniter 2.1.1 in (to start with) the local (Mac OS 10.7) copy of my app. I initially coded it using Active Record for all db operations, and I am now thinking I want to use PDO prepared statements in my model files, going forward.

I modified 'application/config/database.php' like so: (note a couple minor embedded questions)

[snip]

$active_group = 'local_dev';

$active_record = TRUE;//<---BTW, will this need to stay TRUE to make CI sessions work?  For better security, don't we want db-based CI sessions to use PDO too?
//http://codeigniter.com/user_guide/database/configuration.html:
    //Note: that some CodeIgniter classes such as Sessions require Active Records be enabled to access certain functionality.

//this is the config setting that I am guessing (?) is my main problem: 

$db['local_dev']['hostname'] = 'localhost:/tmp/mysql.sock';

// 1.) if $db['local_dev']['dbdriver']='mysql', then here ^^^ 'localhost:/tmp/mysql.sock' works, 2.) but if $db['local_dev']['dbdriver']='pdo', then it fails with error msg. shown below.

$db['local_dev']['username'] = 'root';
$db['local_dev']['password'] = '';
$db['local_dev']['database'] = 'mydbname';
$db['local_dev']['dbdriver'] = 'pdo';
$db['local_dev']['dbprefix'] = '';
$db['local_dev']['pconnect'] = TRUE;
$db['local_dev']['db_debug'] = TRUE;//TRUE
$db['local_dev']['cache_on'] = FALSE;
$db['local_dev']['cachedir'] = '';
$db['local_dev']['char_set'] = 'utf8';
$db['local_dev']['dbcollat'] = 'utf8_general_ci';
$db['local_dev']['swap_pre'] = '';
$db['local_dev']['autoinit'] = TRUE;
$db['local_dev']['stricton'] = FALSE;
[snip]

With the above config., as soon as I load a controller, I get this error message:

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in 
/Library/WebServer/Documents/system/database/drivers/pdo/pdo_driver.php:114 Stack trace: #0 
/Library/WebServer/Documents/system/database/drivers/pdo/pdo_driver.php(114): PDO->__construct('localhost:/tmp/...', 'root', '', Array) #1 /Library/WebServer/Documents/system/database/DB_driver.php(115): CI_DB_pdo_driver->db_pconnect() #2 
/Library/WebServer/Documents/system/database/DB.php(148): CI_DB_driver->initialize() #3 
/Library/WebServer/Documents/system/core/Loader.php(346): DB('', NULL) #4 
/Library/WebServer/Documents/system/core/Loader.php(1171): CI_Loader->database() #5 
/Library/WebServer/Documents/system/core/Loader.php(152): CI_Loader->_ci_autoloader() #6 
/Library/WebServer/Documents/system/core/Con in 
/Library/WebServer/Documents/system/database/drivers/pdo/pdo_driver.php on line 114

I tried swapping out the 'pdo_driver.php' file from the one on github, as per this: http://codeigniter.com/forums/viewthread/206124/ ...but that just generates other errors, not to mention is disturbing to a newbie who does not want to touch the system files if at all possible.

This thread also seems to imply the need to be hacking the 'pdo_driver.php' system file: CodeIgniter PDO database driver not working It seems odd to me, though, that (someone thought that) a hack to a system file is needed to make PDO work in CI v.2.1.1, huh?

Thanks for any suggestions I can try.

Community
  • 1
  • 1
govinda
  • 1,683
  • 5
  • 20
  • 34
  • read here: http://stackoverflow.com/questions/5884761/how-can-i-use-pdo-in-codeigniter-2 – Yan Berk Jun 18 '12 at 06:11
  • thanks Yan, (and I had seen that)... but it looks like that is just old dialogue started by someone who was trying to install a *custom PDO hack* to code igniter, as opposed to what I am trying to do - just get the supported (out of the box) PDO driver running... and so does not help me. – govinda Jun 18 '12 at 14:21

2 Answers2

1

I don't know if this might be helpful for you since you already started using the CI functions, but I made my own library for PDO with sqlite and just auto load it. My needs were simple, so it serves its purpose.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* CodeIgniter PDO Library
*
*
* @author Michael Cruz
* @version  1.0
*/

class Sqlite_pdo
{
    var $DB;

    public function connect($path) {
        try {
            $this->DB = new PDO('sqlite:' . $path);
        }
        catch(PDOException $e) {
            print "Error: " . $e->getMessage();
            die();
        }
    }

    public function simple_query($SQL) {

        $results = $this->DB->query($SQL)
            or die('SQL Error: ' .  print_r($this->DB->errorInfo()));

        return $results;
    }

    public function prepared_query($SQL, $bind = array()) {

        $q = $this->DB->prepare($SQL)
            or die('Prepare Error: ' . print_r($this->DB->errorInfo()));

        $q->execute($bind)
            or die('Execute Error: ' .  print_r($this->DB->errorInfo()));  

        $q->setFetchMode(PDO::FETCH_BOTH);          

        return $q;
    }

    public function my_prepare($SQL) {

        $q = $this->DB->prepare($SQL)
            or die('Error: ' . print_r($this->DB->errorInfo()));            

        return $q;
    }

    public function my_execute($q, $bind) {
        $q->execute($bind)
            or die('Error: ' .  print_r($this->DB->errorInfo()));  

        $q->setFetchMode(PDO::FETCH_BOTH);

        return $q;
    }

    public function last_insert_id() {
        return $this->DB->lastInsertId();
    }
}

/* End of file Sqlite_pdo.php */
MikeCruz13
  • 1,254
  • 1
  • 10
  • 19
  • thanks Mike, I appreciate seeing what knowledgeable people are doing.. (I like seeing your code), but as you noted, I am most interested in just getting the PDO driver running, as now supported by Code Igniter, out of the box. – govinda Jun 18 '12 at 14:24
  • No problem, I didn't actually notice they tried to fix some things with PDO (including supposedly making sqlite work). Nevertheless, the reason I didn't pursue it in 2.10, when they added the driver, is that CI doesn't actually use prepared queries (including their query binding method) and I don't believe adding the PDO driver changed that. – MikeCruz13 Jun 18 '12 at 15:34
  • yeah.. I am reading that too... as I educate myself to what all is going on. Also, (and I have not looked myself to confirm), but I read somewhere in recent days that CodeIgniter-3 is in dev now.. and they are/have overhauled a lot.. including major improvements for the PDO driver. Thus my hesitation to run wild with my own custom stuff (plus I am still somewhat new with SQL). Meanwhile I would love to just get the pdo driver running here. I don't know why something so simple is so elusive so far. – govinda Jun 18 '12 at 17:31
1

thanks to the noob thread http://codeigniter.com/forums/viewthread/180277/ (InsiteFX’s answer)..

I figured out the below seems to work (need to test more to be 100%... but at least the error messages are gone:

$db['local_dev']['hostname'] = 'mysql:host=127.0.0.1'; 
govinda
  • 1,683
  • 5
  • 20
  • 34