CodeIgniter

时间:2018-02-12 11:15:10

标签: php sql codeigniter activerecord codeigniter-2

我正在尝试创建一个基本查询,然后我可以使用它来添加分组或过滤器

简化示例:

function baseQuery()
{
    $query = $this->db->select('*')
    return $query;
}

function queryWhere($value)
{
    $query = $this->baseQuery();
    $query->where($value)
    $result = $query->get();

    return $result
}

在CodeIgniter中执行此操作的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

您的代码需要一些动态值,如表和字段名称,以扩展查询的单一目的 看看示例

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Testing extends CI_Controller {

    public function baseQuery($fields,$tableName)
    {
        $query = $this->db;
        $query->select($fields);
        $query->from($tableName);
        return $query;
    }

    public function queryWhere($whereCondition)
    {
        $query = $this->baseQuery('*','tablename');
        $query->where($whereCondition);
        $result = $query->get();
        return $result;
    }
    public function index() {

        $query = $this->queryWhere("id > 0");
        $data = $query->result_array();
        print_r($data);
    }  

}