在codeigniter中获取不同数据库的select查询结果

时间:2017-04-07 10:44:36

标签: php mysql codeigniter

我在database.php中声明了两个数据库,我如何访问db和db2实例的用户列表

        //database initialisation 
        $CI =& get_instance();
        $CI->db1 = $CI->load->database('default', TRUE);
        $CI->db2 = $CI->load->database('stylior_db', TRUE);
         //select query for db2
    $this->$db2->from('users');
    // $this->$CI->db2->where('id',1312);

    $query = $this->$CI->db2->get();
    print_r($query);

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();
        return $row;
    }  

2 个答案:

答案 0 :(得分:0)

您可以直接使用$ this关键字来获得结果。您可以使用以下代码来获得结果:

//database initialisation 
        $CI =& get_instance();
        $this->db1 = $this->load->database('default', TRUE);
        $this->db2 = $this->load->database('stylior_db', TRUE);
         //select query for db2
    $this->$db2->from('users');
    // $this->db2->where('id',1312);

    $query = $this->db2->get();
    print_r($query);

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();
        return $row;
    }  

答案 1 :(得分:0)

试试这种方式

SELECT
      A.id,
      DATEDIFF(A.date_val,B.date_val) AS duration
      FROM EMP_RECORDS A
      JOIN EMP_RECORDS B
      ON A.id = B.id
WHERE A.PARAM_TYPE = "INPUT"
      B.PARAM_TYPE = "OUTPUT";