Þaw
Þaw

Reputation: 2057

How to execute my SQL query in CodeIgniter

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 from here

Please visit first this link so you could understand why my SQL syntax is like this
http://www.x-developer.com/php-scripts/sql-connecting-multiple-databases-in-a-single-query


Im using CodeIgniter and here is an Idea of what my query looks like:
Notice the way I'm selecting my columns: DATABASE_NAME.TABLE_NAME.COLUMN_NAME

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

$SELECT    = "SELECT $ACCOUNTS.BALANCES_TABLE.IDNO, $ACCOUNTS.BALANCES_TABLE.balance";
$FROM      = "FROM $ACCOUNTS.BALANCES_TABLE";
$WHERE     = "$ACCOUNTS.BALANCES_TABLE.IDNO IN (SELECT $ENROLLEES.ENROLLEES_TABLE.IDNO FROM $ENROLLEES.ENROLLEES_TABLE)";

$SQL       = $SELECT ." ". $FROM ." ". $WHERE;

MAIN PROBLEM: How to Execute my query?
If we do like this in codeIgniter:

$ENROLLEES->query($SQL); or $ACCOUNTS->query($SQL);

How can I execute my query that Im having multiple databases? What will I provide here
[database]->query($SQL); ?

Upvotes: 20

Views: 177490

Answers (7)

Rafi Ahmed
Rafi Ahmed

Reputation: 9

Select Query:
$this->db->select('column1, column2');
$this->db->from('your_table');
$query = $this->db->get();
Insert Query:
$data = array('column1' => 'value1','column2' => 'value2');
$this->db->insert('your_table', $data);
Update Query:
$data = array('column1' => 'new_value1','column2' => 'new_value2');
$this->db->where('your_condition');
$this->db->update('your_table', $data);
Delete Query:
$this->db->where('your_condition');
$this->db->delete('your_table');

Upvotes: -1

Mohit maru
Mohit maru

Reputation: 827

    $sql="Select * from my_table where 1";    
    $query = $this->db->query($sql);
    return $query->result_array();

Upvotes: 43

Gurpreet Singh
Gurpreet Singh

Reputation: 383

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();

Upvotes: 3

user9232865
user9232865

Reputation:

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

       $this->db->from('tbl_books');

Upvotes: 0

Muhammad Sulman
Muhammad Sulman

Reputation: 1681

 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')

Upvotes: 0

Nah
Nah

Reputation: 1768

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

Upvotes: 3

Robin Castlin
Robin Castlin

Reputation: 10996

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.

Upvotes: 13

Related Questions