Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: Using PDO instead of mysql

Until Codeigniter implements the use of PDO, is there a way to use hack it into CI that's stable and secure? Currently, instead of using the db driver, I'm using a model instead which has all my PDO code like prepare, fetch, execute, etc. in it. What are the rest of you doing?

like image 881
enchance Avatar asked Feb 14 '12 21:02

enchance


People also ask

Is php PDO deprecated?

PDO (PHP Data Objects) Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.

How to Configure database in CodeIgniter?

CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database. php. You can also set database connection values for specific environments by placing database.

Does PDO work with MariaDB?

On the other hand, once you've mastered PDO, you can use it with any database you desire, which can be incredibly useful when switching from another database to, say, MariaDB.

How to Config database in CodeIgniter 4?

php namespace Config; use CodeIgniter\Database\Config; class Database extends Config { public $test = [ 'DSN' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'database_name', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => true, 'DBDebug' => true, 'charset' => 'utf8', ' ...


2 Answers

On CodeIgniter 2.1.4+ using MySQL databases (Edit the file: /application/config/databases.php).

To use PDO:

$db['default']['hostname'] = 'mysql:host=localhost';
$db['default']['dbdriver'] = 'pdo';

To use MySQLi

$db['default']['hostname'] = 'localhost';
$db['default']['dbdriver'] = 'mysqli';
like image 120
Thiago Pereira Avatar answered Sep 28 '22 01:09

Thiago Pereira


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

$db['default'] = array(
    'dsn'   => 'mysql:host=127.0.0.1; dbname=****yourdatabasename*****; charset=utf8;',
    'hostname' => '',
    'username' => 'root',
    'password' => '******yourpassword*******',
    'database' => '',
    '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
);
like image 45
Daniel00127 Avatar answered Sep 28 '22 01:09

Daniel00127