php - 使用 codeigniter 在非对象上调用成员函数 num_rows()

标签 php mysql codeigniter

我被这个愚蠢的问题困住了好几次,我知道我可能犯了一些愚蠢的错误,基本上我有一个 Controller 调用模型函数并返回数据然后将它分配到一个数组中,但是我在非对象成员错误时继续调用成员函数 num_rows,

我的 Controller 函数是:

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    if ($alreadyExist == FALSE) 
    {
      // code if nothing found
    } else 
    {
     for ($i=0; $i < $alreadyExist->num_rows(); $i++) 
     { 
       $row = $alreadyExist->row($i);
       $FY_budget_ID[$i] = $row->FY_budget_ID;
     }
    }
}

My model is :

public function checkAlreadyExist($client_block_ID)
{
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $query = $this->db->get('fy_budget');
    if ($query->num_rows() >= 1) 
    {
        return $query;
    } else 
    {
        return FALSE;
    }

}

错误在调用num_rows函数的else部分...

最佳答案

试试这个:

public function checkAlreadyExist($client_block_ID)
{
    $data   = array();
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $data   = $this->db->get('fy_budget')->result_array();
    return $data;
}

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $FY_budget_ID = array();
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    echo $this->db->last_query();
    /*if( isset( $alreadyExist ) && count( $alreadyExist ) > 0 ){
        foreach ( $alreadyExist as $key => $each ) 
        { 
            $FY_budget_ID[$key] = $each['FY_budget_ID'];
        }    
    }*/
    echo "<pre>";print_r( $alreadyExist );
}

关于php - 使用 codeigniter 在非对象上调用成员函数 num_rows(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19559104/

相关文章:

php - 用户名或电子邮件存在时不返回错误

mysql - 在 Netbeans 中找不到 MySQL 连接器

php - 如何使用谷歌浏览器进行控制台

php - 自定义验证设计php、mysql

php - 如何避免在 PHP 7 中的每个文件上重新声明刻度

mysql - 从值列表中进行 sqlite 查询

mysql - 每日、每月或每年报告列出新老用户的 Access 情况

php变量异常行为

php - 当 Controller 类扩展父 Controller 时,为什么我们仍然需要父构造函数?

php - 阅读PDF,TJ算子奇怪的编码