How to execute my SQL query in CodeIgniter

后端 未结 6 1212
执笔经年
执笔经年 2020-11-29 05:42

I have a problem with my query and I need to join two tables from different databases now my problem is how can I execute my query. I got my syntax format f

相关标签:
6条回答
  • 2020-11-29 05:46
     return $this->db->select('(CASE 
                enter code hereWHEN orderdetails.ProductID = 0   THEN dealmaster.deal_name
                WHEN orderdetails.DealID = 0 THEN products.name
                END) as product_name')
    
    0 讨论(0)
  • 2020-11-29 05:51

    http://www.bsourcecode.com/codeigniter/codeigniter-select-query/

    $query = $this->db->query("select * from tbl_user");
    

    OR

    $query = $this->db->select("*");
                $this->db->from('table_name');
                $query=$this->db->get();
    
    0 讨论(0)
  • 2020-11-29 05:53

    $this->db->select('id, name, price, author, category, language, ISBN, publish_date');

           $this->db->from('tbl_books');
    
    0 讨论(0)
  • 2020-11-29 06:03

    I can see what @Þaw mentioned :

    $ENROLLEES = $this->load->database('ENROLLEES', TRUE);
    $ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
    

    CodeIgniter supports multiple databases. You need to keep both database reference in separate variable as you did above. So far you are right/correct.

    Next you need to use them as below:

    $ENROLLEES->query();
    $ENROLLEES->result();
    

    and

    $ACCOUNTS->query();
    $ACCOUNTS->result();
    

    Instead of using

    $this->db->query();
    $this->db->result();
    

    See this for reference: http://ellislab.com/codeigniter/user-guide/database/connecting.html

    0 讨论(0)
  • 2020-11-29 06:09
        $sql="Select * from my_table where 1";    
        $query = $this->db->query($SQL);
        return $query->result_array();
    
    0 讨论(0)
  • 2020-11-29 06:09

    If the databases share server, have a login that has priveleges to both of the databases, and simply have a query run similiar to:

    $query = $this->db->query("
    SELECT t1.*, t2.id
    FROM `database1`.`table1` AS t1, `database2`.`table2` AS t2
    ");
    

    Otherwise I think you might have to run the 2 queries separately and fix the logic afterwards.

    0 讨论(0)
提交回复
热议问题