【问题标题】:Code Igniter - error when trying to config database.php to use PDO driverCode Igniter - 尝试配置 database.php 以使用 PDO 驱动程序时出错
【发布时间】:2012-06-20 01:09:08
【问题描述】:

我正在尝试在我的应用程序的本地 (Mac OS 10.7) 副本中(开始)在 Code Igniter 2.1.1 中运行新的 PDO 驱动程序。 我最初使用 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 database driver not working 不过,对我来说似乎很奇怪,(有人认为)需要对系统文件进行 hack 才能使 PDO 在 CI v.2.1.1 中工作,对吧?

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

【问题讨论】:

  • 感谢 Yan,(我已经看到了)...但看起来这只是由试图安装 custom PDO hack 的人开始的旧对话代码点火器,而不是我想要做的 - 只是让受支持的(开箱即用)PDO驱动程序运行......所以对我没有帮助。

标签: codeigniter configuration


【解决方案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 */

【讨论】:

  • 感谢 Mike,我很高兴看到知识渊博的人在做什么。(我喜欢看你的代码),但正如你所说,我最感兴趣的是让 PDO 驱动程序运行,因为现在支持Code Igniter,开箱即用。
  • 没问题,我实际上并没有注意到他们试图用 PDO 修复一些东西(包括据说让 sqlite 工作)。尽管如此,当他们添加驱动程序时,我在 2.10 中没有追求它的原因是 CI 实际上并没有使用准备好的查询(包括他们的查询绑定方法),我不相信添加 PDO 驱动程序会改变这一点。
  • 是的..我也在读那本书...因为我正在教育自己了解正在发生的事情。此外,(而且我还没有自己确认),但最近几天我在某处读到 CodeIgniter-3 现在正在开发中......并且他们已经/已经大修了很多......包括对 PDO 驱动程序的重大改进。因此,我犹豫是否要使用自己的自定义内容(而且我对 SQL 还是有些陌生)。同时,我很想在这里运行 pdo 驱动程序。我不知道为什么这么简单的事情到目前为止如此难以捉摸。
【解决方案2】:

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

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

$db['local_dev']['hostname'] = 'mysql:host=127.0.0.1'; 

【讨论】:

    猜你喜欢
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2013-10-06
    • 1970-01-01
    • 2015-08-08
    • 2014-08-21
    • 1970-01-01
    相关资源
    最近更新 更多