Search code examples
phpzend-frameworkzend-db

Why translate sql queries into Zend_Db style?


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.


Solution

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