1

I would like to ask why people use zend_db style?

e.g.

     $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.

1
  • Both have advantages and disadvantages. Commented Jul 12, 2012 at 20:51

2 Answers 2

6

One reason is that using Zend_db style abstracts out the generation of 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. It also provides database querying functionality for those who do not know SQL.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks! sorry, my english isnt that good. When you say abstract out, do you mean we do not need to specially cater for sql injection(like addslashes etc) when we adopt zend_db style? It's done automaticaly on zend db classes?
Abstraction is basically "something else does the work for you", usually a simple, repeatable task. For instance, the + operator is an abstraction of the addition function. The function itself actually manipulates a bunch of bits in memory, but that's something you don't have to do because it's been abstracted out for you.
5

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, e.g.:

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

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

1 Comment

+1 plus what Richard Ye mentioned (portability between the RDBMSs).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.