Codeigniter PDO 集成 [英] Codeigniter PDO integration

查看:35
本文介绍了Codeigniter PDO 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i did lot of research on the web but i didnt find anything that could help me to use PDO in codeigniter. I saw in the change lof of CI 2.1.0(i think) that pdo driver was added to the framwork. I ended up now with a database.php config file that looks like this:

$db['default']['hostname'] = 'mysql:host=myhostname;port=myport'; 
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword'; 
$db['default']['database'] = 'mydb'; 
$db['default']['dbdriver'] = 'pdo';

So now(after a lot of wasted time to get the snippet above to work -.- ) i receive no error about connection, but HOW TO EXECUTE QUERY NOW? i cant figure out what syntax will work and how to build queries. Anyone have hints?

PS: if you're wordering about why i need pdo in ci, the answer is my boss want me to create a structured enviroment with:

  1. CI 2.x + (done)
  2. Smarty 3 (done)
  3. PhpUnit (not yet)
  4. PDO (not yet)

so if you have also any hints for integrate phpunit feels free to answer. Ty in advance

解决方案

You use PDO the same way you use any other database driver in CodeIgniter. If you are still unsure then I would recommend reading the documentation on the Database Class.

You can issue standard queries by explicitly writing the query or you can use the Active Record Class (which is more of a query builder).

Here are some examples:

// Standard query
$results = $this->db->query('SELECT name, title, email FROM my_table');

// Active record
$this->db->select('name, title, email');
$results = $this->db->get('my_table');

As for integrating PHPUnit, have a look at https://github.com/cmillr/CI-PHPUnit (I haven't tested it myself) or look around the CodeIgniter forums. I've seen a ton of topics on integrating PHPUnit with CodeIgniter.

这篇关于Codeigniter PDO 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆