代码之家  ›  专栏  ›  技术社区  ›  Glen Solsberry

Zend_-DB查询和行数,而不将所有内容都拉回到前面

  •  1
  • Glen Solsberry  · 技术社区  · 15 年前

    我已经创建了我的选择:

    $select = $zdb->select()
                  ->from(array("b" => "blogs"),
                         array("id", "active", "updated_by", "title", "synopsis", "create_date", "body"))
                  ->join(array("u" => "users"),
                         'b.updated_by = u.id',
                         array("first_name", "last_name"))
                  ->where("u.blogger = ?", 1)
                  ->where("b.publish_date > ?", '2020-01-01')
                  ->where("b.active = ?", 1)
                  ->group("b.id")
                  ->order("b.publish_date DESC")
                  ->limit(5);
    

    我想一次把数据拉回一行:

    $stmt = $db->query($select);
    
    while ($asset = $stmt->fetch()) {
        // do stuff
    }
    

    如何检查以确保有行,而不返回整个结果集?

    1 回复  |  直到 15 年前
        1
  •  1
  •   JF Dion    15 年前

    使用已有的select,类似这样的方法可以帮助您解析每个条目。

    $rows = $zdb->fetchAll($select);
    
    foreach($rows as $row){
        ...
    }
    

    要获取值,只需执行$row['field name'],其中fieldname是数据库中字段的名称。