如何将PDO与codeignitier 3一起使用

时间:2015-09-05 17:01:17

标签: php mysql codeigniter csv pdo

Codeignitier向我展示了#34; loading"来自echo,但当我尝试使用PDO执行此操作时表格为空:

function loadCSVtoDB($csvfile){
    $stmt = $this->db->conn_id->prepare("LOAD DATA LOCAL INFILE :file
    INTO TABLE `historic` FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");
    $stmt->bindParam(":file",$csvfile);
    $stmt->execute();
    echo 'loaded';
    //echo $this->db->conn_id->error_message();
}

我的数据库配置是:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'mysql:host=localhost',
    'username' => 'zzazfhvsnt',
    'password' => 'censored',
    'database' => 'zzazfhvsnt',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

当我删除 conn_id 时,我会

  

致命错误:调用未定义的方法   CI_DB_pdo_mysql_driver ::制备()

同样的,当我改变exec而不是准备。

我从构造函数中调用它:

$this->load->model('Historic_model');
$this->Historic_model->loadCSVtoDB("assets/csv/1516E0.csv");    

在自动加载中是:$autoload['libraries'] = array('database');

1 个答案:

答案 0 :(得分:0)

尝试使用以下'dsn' => 'mysql:host=localhost; dbname=myproject; charset=utf8;',

下面的代码

然后自动加载数据库库

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'  => 'mysql:host=localhost; dbname=myproject; charset=utf8;',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '*********',
    'database' => '',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '' ,
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);