简体   繁体   中英

How to use PDO with codeignitier 3

Codeignitier shows me just "loaded" from echo, but table is empty when i try to execute this using 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();
}

My database config is:

$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
);

When i remove conn_id , i get

Fatal error: Call to undefined method CI_DB_pdo_mysql_driver::prepare()

Same is when i change exec instead prepare.

I call it like this from constructor:

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

In autoload is: $autoload['libraries'] = array('database');

Try with code below 'dsn' => 'mysql:host=localhost; dbname=myproject; charset=utf8;', 'dsn' => 'mysql:host=localhost; dbname=myproject; charset=utf8;',

Then autoload the database library

$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
); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM