Codeigniter: using PDO instead of mysql

Until Codeigniter implements the use of PDO, is there a way to use it to crack in CI, which is stable and secure ? Currently, instead of using the db driver, I am using instead a model that has all my PDO code, for example prepare , fetch , execute , etc. What are you doing?

+4
source share
6 answers

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

Use PDO:

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

Use MySQLi

 $db['default']['hostname'] = 'localhost'; $db['default']['dbdriver'] = 'mysqli'; 
+4
source

CI, if used correctly, is reliable and safe. Using PDO, although it is better if you do not use the framework, does not necessarily have a beneficial effect on the CI_Database class.

If this really bothers you, you can change the mysql_*() functions for the equivalent mysqli_*() functions, but it really won’t make any noticeable difference unless you optimize the hyper help.


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

+3
source

Using PDO drivers instead of mysql requires changing the host name and dbdriver as follows:

 $db['default']['hostname'] = 'mysql:host=localhost'; $db['default']['dbdriver'] = 'pdo'; 
+3
source
 $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 ); 
+2
source

try php-activerecord I believe this uses the PDO driver, its simple plug and play through sparks.

0
source

Just follow whoever has the same problem (including my future), make sure that depending on which dbdriver you are loading, there is a valid 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 crash with a blank page.

0
source

Source: https://habr.com/ru/post/1396496/


All Articles