php Codeigniter:使用 PDO 而不是 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9284274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:29:44  来源:igfitidea点击:

Codeigniter: Using PDO instead of mysql

phpcodeigniterpdo

提问by enchance

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?

在 Codeigniter 实现 PDO 的使用之前,有没有办法将其破解为稳定且安全的CI ?目前,而是采用了数据库驱动程序,我使用的,而不是一个模型,它具有像我所有的PDO的代码preparefetchexecute等它。你们其他人在做什么?

回答by Thiago Pereira

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

CodeIgniter 2.1.4+ 上使用MySQL数据库(编辑文件:/application/config/databases.php)。

To use PDO:

使用 PDO:

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

To use MySQLi

使用 MySQLi

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

回答by Daniel00127

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

回答by Mathieu F.

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

使用 PDO 驱动程序而不是 mysql 需要像这样更改主机名和 dbdriver:

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

回答by cwallenpoole

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.

如果使用得当,CI 既可靠又安全。使用 PDO,虽然如果您不使用框架会更好,但与 CI_Database 类相比,您不一定会受益匪浅。

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.

如果它真的困扰您,您可以将mysql_*()功能替换为等效mysqli_*()功能,但除非您进行超优化,否则它确实不会提供任何可辨别的差异。



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

应该注意的是,这实际上可以通过适当地设置 dbtype 自动完成(如下面的 Rocket 注释)。

回答by Eric Kigathi

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

只是对遇到相同问题的任何人(包括我未来的自己)进行跟进,请确保您正在加载的任何 dbdriver 在 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.

未能加载正确的 dll 将导致 CodeIgniter 失败并显示空白页面。

回答by Philip

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

试试php-activerecord我相信这个使用的 PDO 驱动程序,它是一个简单的即插即用的火花。