0

我试图让新的 PDO 驱动程序在我的应用程序的本地(Mac OS 10.7)副本中(开始)在 Code Igniter 2.1.1 中运行。我最初使用 Active Record 对所有数据库操作进行编码,现在我想我想在我的模型文件中使用 PDO 准备语句,继续前进。

我像这样修改了“application/config/database.php”:(注意几个小的嵌入式问题)

[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]

使用上述配置,一旦我加载控制器,我就会收到以下错误消息:

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

我尝试从 github 上的文件中换出“pdo_driver.php”文件,如下所示:http://codeigniter.com/forums/viewthread/206124/ ... 但这只会产生其他错误,更不用说令人不安了如果可能的话,对于不想接触系统文件的新手。

该线程似乎还暗示需要破解“pdo_driver.php”系统文件: CodeIgniter PDO 数据库驱动程序不起作用 不过,对我来说似乎很奇怪,(有人认为)需要对系统文件进行破解PDO 在 CI v.2.1.1 中工作,对吧?

感谢我可以尝试的任何建议。

4

2 回答 2

1

我不知道这是否对您有帮助,因为您已经开始使用 CI 函数,但是我使用 sqlite 为 PDO 创建了自己的库,然后自动加载它。我的需求很简单,所以它达到了它的目的。

<?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 */
于 2012-06-18T06:16:05.407 回答
1

感谢菜鸟线程http://codeigniter.com/forums/viewthread/180277/(InsiteFX的回答)..

我发现以下似乎可行(需要测试更多才能达到 100% ......但至少错误消息消失了:

$db['local_dev']['hostname'] = 'mysql:host=127.0.0.1'; 
于 2012-06-21T01:01:16.707 回答