如何在 CodeIgniter 2 中使用 PDO?

发布于 2024-11-05 15:28:07 字数 667 浏览 0 评论 0原文

我想使用 PDO 而不是内置数据库类。但我该怎么办呢?我尝试按照博客文章中建议的修改进行操作。我注释掉了 system\database\DB.php 的一部分,并添加了:

$DB = new PDO(
$params['dbdriver'].':host='.$params['hostname'].';dbname='.$params['database'],
$params['username'], $params['password']);

return $DB;

但是现在如果我执行 $this->load->database(); ,我的 PHP 代码就会崩溃。 在控制器中。那么我应该如何使用PDO并在CodeIgniter中使用PDO执行数据库查询呢?

我也尝试在控制器中使用此代码:

foreach($this->db->query('SELECT * FROM users') as $row) {
    print_r($row);
}

但它不起作用。

I would like to use PDO instead of the built-in database-classes. But how can I do it? I tried to do the modifications suggested in a blog post. I commented out a part of system\database\DB.php and also added:

$DB = new PDO(
$params['dbdriver'].':host='.$params['hostname'].';dbname='.$params['database'],
$params['username'], $params['password']);

return $DB;

But now my PHP-code crashes if I do $this->load->database(); in a Controller. So how should I use PDO and execute database queries using PDO in CodeIgniter?

I have also tried to use this code in a Controller:

foreach($this->db->query('SELECT * FROM users') as $row) {
    print_r($row);
}

but it didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-11-12 15:28:07
$stmt = $this->db->prepare("SELECT * FROM users");  
$stmt->execute();  
foreach ($stmt->fetch(PDO::FETCH_ASSOC) as $row) {
      print_r($row);
}

或者:

foreach ($this->db->query("SELECT * FROM users") as $row) {  
     print_r($row)."\n";
}    

在此处了解有关 3 个 PDO 数据库调用的更多信息...

PDO 查询
PDO 执行
PDO 准备

编辑:另请检查您的 app/config/database.php 文件进行以下设置:

$active_group = 'default';  
$active_record = FALSE;  

$db['default']['hostname'] = 'YOURHOSTNAME';  
$db['default']['username'] = 'YOURUSERNAME';  
$db['default']['password'] = 'YOURPASSWORD';  
$db['default']['database'] = 'YOURDATABASE';  
$db['default']['dbdriver'] = 'mysql';  
$db['default']['dbprefix'] = '';  
$db['default']['pconnect'] = TRUE;  
$db['default']['db_debug'] = TRUE;  
$db['default']['cache_on'] = FALSE;  
$db['default']['cachedir'] = '';  
$db['default']['char_set'] = 'utf8';  
$db['default']['dbcollat'] = 'utf8_general_ci';  
$db['default']['swap_pre'] = '';  
$db['default']['autoinit'] = TRUE;  
$db['default']['stricton'] = FALSE; 
$stmt = $this->db->prepare("SELECT * FROM users");  
$stmt->execute();  
foreach ($stmt->fetch(PDO::FETCH_ASSOC) as $row) {
      print_r($row);
}

Or:

foreach ($this->db->query("SELECT * FROM users") as $row) {  
     print_r($row)."\n";
}    

Learn more about 3 PDO database calls here...

PDO Query
PDO Exec
PDO Prepare

EDIT: Also check your app/config/database.php file for the following settings:

$active_group = 'default';  
$active_record = FALSE;  

$db['default']['hostname'] = 'YOURHOSTNAME';  
$db['default']['username'] = 'YOURUSERNAME';  
$db['default']['password'] = 'YOURPASSWORD';  
$db['default']['database'] = 'YOURDATABASE';  
$db['default']['dbdriver'] = 'mysql';  
$db['default']['dbprefix'] = '';  
$db['default']['pconnect'] = TRUE;  
$db['default']['db_debug'] = TRUE;  
$db['default']['cache_on'] = FALSE;  
$db['default']['cachedir'] = '';  
$db['default']['char_set'] = 'utf8';  
$db['default']['dbcollat'] = 'utf8_general_ci';  
$db['default']['swap_pre'] = '';  
$db['default']['autoinit'] = TRUE;  
$db['default']['stricton'] = FALSE; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文