Home > Database > Mysql Tutorial > body text

Zend_Db_Expr

WBOY
Release: 2016-06-07 16:02:51
Original
1039 people have browsed it

在zendframework中使用Db类时,框架会自动给sql语句添加引号以防止数据库攻击 ,这就导致了一个问题,用户无法使用zend db类使用mysql的内置函数(方法,存储过程等)。好在zend框架提供了一个类Zend_Db_Expr,此类的构造函数会告诉框架不要对它所转化的类型

在zendframework中使用Db类时,框架会自动给sql语句添加引号以防止数据库攻击 ,这就导致了一个问题,用户无法使用zend db类使用mysql的内置函数(方法,存储过程等)。好在zend框架提供了一个类Zend_Db_Expr,此类的构造函数会告诉框架不要对它所转化的类型进行添加引号的操作。

    public function setBatchDelete($orderIds) {
        $flag = YCL_Order::FLAG_DELETED;
        $updateTime = time();
        $data = array(
            'flag' => new Zend_Db_Expr("flag | $flag"),
            'update_time' => $updateTime
        );   
        $db = $this->getAdapter();
        $where = $db->quoteInto('service_order_id IN (?)',$orderIds);
        $this->update($data,$where);
    }
Copy after login

上面的例子 左边的flag是数据库中的flag。

   $select=$db->select();
   $select->from("testtable","*");
   $select->where($db->quotInto("date>=?",new Zend_Db_Expr("UNIX_TIMESTAMP()"));
   $db->fetchAll($select);
Copy after login
框架不会对UNIX_TIMESTAMP()添加引号,从而让用户能正常使用此函数。
You also can create an object of type Zend_Db_Expr explicitly, to prevent a string from being treated as a column name.
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!