简体   繁体   English

为什么将sql查询转换为Zend_Db样式?

[英]Why translate sql queries into Zend_Db style?

I would like to ask why people use zend_db style? 我想问一下为什么人们使用zend_db样式?

eg 例如

     $select = $d2b->select()
             ->from('users', array('userid','username'))
             ->where('userid=?', 2);

 $result = $d2b->fetchRow($select);
 echo $result->username;

instead of 代替

$result = $d2b->fetchAll('SELECT * FROM users WHERE userid = ?', 2);
echo $result[0]->username;

Which one is better? 哪一个更好? Or is it the same, just for maintainability. 还是一样,只是为了可维护性。

One reason is that using Zend_db style abstracts out the generation of SQL; 原因之一是使用Zend_db样式可以抽象出SQL的生成。 this means that we switching between database engines can be as easy as swapping out a class, rather than having to re-write incompatible queries. 这意味着我们在数据库引擎之间进行切换就像交换一个类一样容易,而不必重写不兼容的查询。

Furthermore, Zend_db abstracts out SQL query escaping, greatly reducing the risk of SQL injection attacks. 此外,Zend_db抽象化了SQL查询转义,从而大大降低了SQL注入攻击的风险。 It also provides database querying functionality for those who do not know SQL. 它还为不了解SQL的用户提供数据库查询功能。

The end result is the same, so it's down to your personal preferences on readabiliy and maintainability. 最终结果是相同的,因此取决于您在可读性和可维护性方面的个人偏好。

The one advantage of the fluent interface style is that it makes it easier to build the queries up programatically, eg: 流利的界面样式的一个优点是,它可以更轻松地以编程方式构建查询,例如:

$select = $d2b->select()
         ->from('users', array('userid','username'));

if ([some condition]) {
    $select->where('userid = ?', 2);
} else {
    $select->where('userid = ?', 62);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM