【问题标题】:get most actual row with zend's fetchRow使用 zend 的 fetchRow 获取最实际的行
【发布时间】:2014-01-30 21:43:37
【问题描述】:

我对 zend (1.12) 很陌生..所以请原谅我的基本问题:

我只想从数据库中获取一行。所以我想像这样使用fetchRow(..)函数

$row = $db->fetchRow($db->select()->where("col1 = '".val1."' AND col2='".val2."'")); 

问题是,可能有很多行适合 where 子句,而我只想获得具有最高 id 的行。我该怎么做?

【问题讨论】:

    标签: zend-framework zend-db


    【解决方案1】:

    fetchRow() 方法只返回一行。如果你想选择符合其他条件的最高ID的行,这样调用它:

    $select = $db->select()
                 ->where('col1 = ?', $val1)
                 ->where('col2 = ?', $val2)
                 ->order('id DESC');
    $row = $db->fetchRow($select);
    

    另外,请记住以上述代码中的方式将值传递给 SQL 查询(以避免 SQL 注入攻击风险)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多