Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute my SQL query in CodeIgniter

Tags:

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

like image 365
Þaw Avatar asked May 08 '13 07:05

Þaw


1 Answers

    $sql="Select * from my_table where 1";    
    $query = $this->db->query($SQL);
    return $query->result_array();
like image 137
Mohit maru Avatar answered Sep 17 '22 05:09

Mohit maru