【问题标题】:Zend Framework: Give result backZend 框架:返回结果
【发布时间】:2013-11-12 09:06:14
【问题描述】:

我将返回 MySQL 查询的结果。 但他返回了 MySQL 查询。

public function getLatestId() {
        $db = Zend_Db_Table::getDefaultAdapter();
        $db->getConnection();
        $result = $db->select()->from("raw_data", array(new Zend_Db_Expr("MAX(id) AS locationname")));
        return $result;
    }

【问题讨论】:

    标签: php mysql zend-framework frameworks


    【解决方案1】:

    $result 的 select 查询语句不是您获取结果所需的结果。尝试以下几行:

    $result = $db->select()->from("raw_data", array(new Zend_Db_Expr("MAX(id) AS locationname")));
    $row = $this->fetchRow($result);
    return $row->toArray();
    

    我正在使用以下函数来获取最大 id 作为 maxItemNumber

    function getLatestId(){
        $select = $this->select()
            ->from('raw_data', array(new Zend_Db_Expr("MAX(id) AS maxItemNumber")));
    
        $row = $this->fetchRow($select);
        if(!$row){
            return 0;
        }
    
        $row = $rows->toArray();
        return $row['maxItemNumber'];
    }
    

    如果您想在插入后立即获取 lastInsertedID,则使用 return 和 insert

    return $this->insert($data);
    

    【讨论】:

    • 你在使用扩展 Zend_Db_Table_Abstract 的 DbTable 类吗?
    • 是的。我的班级:class Application_Model_Mysqldata extends Zend_Db_Table_Abstract { public function getLatestId() { $db = Zend_Db_Table::getDefaultAdapter(); $db->getConnection(); $result = $db->select()->from("data_raw", array(new Zend_Db_Expr("MAX(id) AS Locationname"))); } }
    • 检查我编辑的函数。如果您收到错误,请在此处显示
    【解决方案2】:

    这是因为您正在构建和下一个返回查询...但不执行此查询。 Harish Singh 明确地展示了这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2013-01-28
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多