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?
PDO (PHP Data Objects) Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.
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.
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.
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', ' ...
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';
$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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With