8

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?

enchance
  • 29,075
  • 35
  • 87
  • 127
  • Using C.I's built in functions. CI is known for its speed and lightness, whats your reason on not using it?. – tomexsans Feb 14 '12 at 21:19
  • C.I. doesn't use PDO at all? What do they use for portability? Does the MySQL adapter really use `mysql_*()` functions? – Mike B Feb 14 '12 at 21:21
  • I'm pretty sure CodeIgniter uses PDO under the hood. EDIT: The MySQLi driver uses `mysqli_*`. They have a [PDO Driver](https://github.com/EllisLab/CodeIgniter/blob/develop/system/database/drivers/pdo/pdo_driver.php) you can use. EDIT2: Check [this question](http://stackoverflow.com/questions/8743943/codeigniter-pdo-integration). – gen_Eric Feb 14 '12 at 21:35
  • 1
    For portability CI has an abstract DB API (`CI_Database`) with db-specific functionality implemented in drivers. The MySQL driver uses `mysql_*`. There are also `MySQLi` and `PDO` drivers. None of them use prepared statements because parameter-placeholder escaping is done by CI already. – Francis Avila Feb 14 '12 at 21:41

6 Answers6

9

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';
Nate
  • 18,752
  • 8
  • 48
  • 54
  • 3
    With CodeIgniter 3+, you now have to use `$db['default']['hostname'] = 'mysql:host=localhost;dbname=database_name';` or you get en error stating "No database selected". – SteeveDroz Mar 03 '19 at 06:29
8
$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
);
Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29
4

Using PDO drivers instead of mysql require to change the hostname and the dbdriver like this :

$db['default']['hostname'] = 'mysql:host=localhost';
$db['default']['dbdriver'] = 'pdo';
Mathieu F.
  • 41
  • 1
3

CI, if used correctly, is both reliable and safe. Using PDO, while better if you are not using a framework, doesn't necessarily benefit you terribly over the CI_Database class.

If it really bothers you, you can swap out the mysql_*() functions for the equivalent mysqli_*() functions, but it really won't provide any discernable difference unless you are hyper-optimizing.


It should be noted that this can actually be done automatically by setting the dbtype appropriately (as Rocket notes below).

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

Just a follow up to anyone having this same issue (including my future self), please ensure that whichever dbdriver you are loading has the correct library loaded in php.ini

$db['default']['dbdriver'] = 'mysqli'; //MySQLi <-- mysqli.dll
$db['default']['dbdriver'] = 'mysql'; //MySQL <-- mysql.dll
$db['default']['dbdriver'] = 'pdo'; //PDO <-- pdo.dll

Failure to load the correct dll will cause CodeIgniter to fail with a blank page.

Eric Kigathi
  • 1,815
  • 21
  • 23
-1

try php-activerecord I believe this use's PDO driver, its a simple plug and play via sparks.

Philip
  • 4,592
  • 2
  • 20
  • 28