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); ?
$sql="Select * from my_table where 1";
$query = $this->db->query($SQL);
return $query->result_array();
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.
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
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();
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')
$this->db->select('id, name, price, author, category, language, ISBN, publish_date');
$this->db->from('tbl_books');
Related
I have a get parameter and I want to select from my table based on this get parameter, But I have to check the two conditions after where if the first was not equal then move to the next one. But the main problem is I want to know which condition was executed the first one or the second one.
$sql = $db_->prepare("Select * from table_x where url_en = ? Or url_fr = ?");
Instead of doing this:
$sql = $DB_->prepare("select * from tbl_items where `url_ar`= ?");
$sql->bind_param("s", $_GET["url"]);
$sql->execute();
$result = $sql->get_result();
if($result->num_rows == true){$lang = "ar";}else{
$sql = $DB_->prepare("select * from tbl_items where `url_fr`= ?");
$sql->bind_param("s", $_GET["url"]);
$sql->execute();
$result = $sql->get_result();
if($result->num_rows == true){$lang = "fr";}else{
echo "unfound";exit;
}
}
I want something short by using just SQL
Try searching up each value independently or try bringing them up together in a list and see where they reside when trying to organize it with ORDER BY.
You can use UNION and execute each condition separately. Using some kind of label you can then find out which condition was true.
SELECT 'EN' as label, * FROM table_x WHERE url_en = ?
UNION
SELECT 'FR' as label, * FROM table_x WHERE url_fr = ?
Good Day Folks,
I have a children_tb and I JOIN residents_tbl to it. Then, on residents_tbl I created a view named family_head.
Is it possible to use family_head on another JOIN statement to children_tbl query statement?
Current query
$query = $this->db->select('*');
$query = $this->db->from('children_tbl');
$query = $this->db->join('residents_tbl', 'residents_tbl.residentID=children_tbl.childName', 'full');
---> ***I want to add another join here for view (family_head)***
$query = $this->db->where('childName', $id);
$query = $this->db->order_by('childID', 'DESC');
$query = $this->db->get('');
I try to use
$query = $this->db->join('family_head', 'family_head.residentID=children_tbl.familyHeadID', 'full');
but I got an error message:
Table 'db_name.family_head' doesn't exist
TIA,
C.M.
This issue was already solved.
table names are just mismatched.
Currently I will use two queries to select data from different databases.
Here´s the main sections from the code..
query from database 1
mysql_select_db("data1");
mysql_select_db("data2");
//bought databases connected
$query1 = "select * from ex.1 where value1='".$value1."' and value2='".$value2."' order by number ASC;";
$run1 = mysql_query($query1);
query from database 2
$query2= " select * from ex.2 where value1='".$value1."' and value2='".$value2."' order by number ASC;";
$run2 = mysql_query($query2);
Both tables in different databases will include same information from the strings "$value1" and "$value2".
I would like to combine these two strings in one query. Meaning that all information from two different databases will be selected.
I tried to use JOIN the following way:
$query = "select * from database1.ex.1 JOIN database2.ex.2 where value1='".$value1."' and value2='".$value2."' order by number ASC;";
$run = mysql_query($query);
but no success..
Meaning from all this is that I would like to echo mysql-data to the same table from two different database.
while ($row=mysql_fetch_array($run)){
echo '</tr><td>'.($row['something']).'</td><td>'.($row['more']).'</td><td>'.($row['and more']).'</td><td>'.($row['and more']).'</td><td>'.($row['and something from the database2']);
This way all information should be echoed nicely, following the few strings which will found from both databases.
Is this understandable?
Instead of using mysql_* for your PHP scripts, you should use MySQLI as mysql is being deprecated.
$db = new mysqli(host, user, password, database;
You should also escape the strings to be more secure and check to see if the connection has been successful as well:
$dbhost = "xxx.xxx.xxx.xxx";
$dbuser = "xxxxxxx";
$dbpass = "xxxxxx";
$dbbase = "xxxxxx";
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbbase);
if($db->connect_errno) {
die('Could not connect to the database: ' . $db->connect_error);
}
You can follow this and repeat for multiple databases!
You have the join part right, but you have got the clause where instead of on:
$query = "SELECT * FROM database1.ex.1 as db1 JOIN database2.ex.2 as db2
ON(db1.value1='".$value1."' and db2.value2='".$value2."') order by number ASC;";
You can change db1.value1/db1.value2 for db2.whatever.
After reviewing this SO, I am having some trouble using prepared statements.
I have added the following to my db connection:
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
And here are my query statements:
$query = "SELECT schedules.schedule_name, users.name, schedules.schedule_id
FROM schedules
INNER JOIN users
ON schedules.admin_id=users.user_id
WHERE schedules.schedule_name
LIKE '%:search%'
ORDER BY schedules.schedule_name";
$stmt = $db->prepare($query);
$result = $stmt->execute(array('search' => $search_string));
$search_results = $stmt->fetchAll();
As you can see in the LIKE, I am trying to replace :search when the query is executed but it does not seem to be replaced.
When you do a parameterized query, it is not simply a string replace, and you don't surround it with singlequotes:
$query = "SELECT schedules.schedule_name, users.name, schedules.schedule_id
FROM schedules
INNER JOIN users
ON schedules.admin_id=users.user_id
WHERE schedules.schedule_name
LIKE :search
ORDER BY schedules.schedule_name";
But you want to have % wildcards around the term right? Add them when you inject it:
$result = $stmt->execute(array('search' => '%' . $search_string . '%' ));
In zend it is written like
$table = $this->getTable();
$select = $table->select()->where('status = ?',1)
->where('columnOne= ?', 1)
->order('columnTwo')
->limit(1);
similar to where, order, limit conditions how can I condition for LIKE?
My query is
SHOW TABLE STATUS LIKE 'tableName'
I tried in this way
$table = $this->getTable();
$query= $table->select("TABLE STATUS")
->like($table);
$id = mysql_query($query);
then I found that no method for LIKE is available in ZEND.
Then How can I write above query in Zend framerk?
Thanks in advance.
This works for me, so hopefully it should give you what you want:
$stmt = $dbAdapter->query("SHOW TABLE STATUS LIKE '$tableName';");
$tableStatus = $stmt->fetchObject();