开发者

Why can't we use :key => value in CodeIgniter's DB library?

I've used to use something like this with the regular PHP's PDO:

$data = array(
    ":name" => "james",
    ":location" => "Palo Alto, CA"
);

SQL:

SELECT * FROM people WHERE name LIKE :name and location = :location

When i've started to use Codeigniter, it won't let me use namespace keys anymore, it开发者_开发百科 only accepts the traditional ? marks.

Anyway to fix that?


Unfortunately, no, there isn't. Not with CodeIgniter natively.

It helps to remember that CodeIgniter's roots are in PHP4 compliant code (and some of the things they did are not even the most recent PHP 4 -- they use a custom file searching system which is substantially slower than glob which was around by PHP 4.3 (4.4? It was around for the minimum required version), this means that the old '?' was really the best option at the time.

If you feel better about using the newer style, then you might be better off using the PDO classes. They're better and faster anyway. (Frankly, I only use CI's DB classes for compliance. I have a very strong preference for PDO's, especially since all of the modern frameworks seem to use them). I will warn you, though, that the use of PDO's completely invalidates the ActiveRecord framework offered by CodeIgniter. You will not be able to use $this->db->select()->from('table')->where($array)->limit(1,4); More importantly, you will need to know the differences between the different dialects of SQL, something CodeIgniter lets you avoid (and your code won't be DB agnostic anymore).


Maybe you will be more comfortable using Active Record in Codeigniter and doing something like

$this->db->like();

Look here: http://codeigniter.com/user_guide/database/active_record.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜