I tried to run query on multiple database with following sql query:
// Collect clients db name
$dbs = array();
foreach($cafes as $cafe)
{
$dbs[] = $cafe->db_name; // client_7
}
// Run multi database query
foreach($dbs as $db)
{
$this->db->select("$db.orders.*,(select count($db.order_items.id) from $db.order_items where $db.order_items.siparis_id = orders.id) as urun_sayisi, (select sum($db.order_items.tutari) from $db.order_items where $db.order_items.siparis_id = orders.id) as price");
}
$this->db->get()->result();
In above example, i have cafe clients and each client have own db named ex: client_7. I am trying to do list all client orders. I got following error with above query:
Unknown table 'client_7' in field list
How can list all rows on orders table from multiple database?
Please mention database name ahead your table name..
for example
SELECT userid FROM db1.user;
SELECT userid FROM db2.user;
Try this:
<?php
$secound_db= $this->load->database('database_two', TRUE);
$query = $secound_db->get('person');
var_dump($query);
Source
Related
Im searching for a function in MySQL in order to Select rows from multiple tables that have a similar name. For example: Proyect_1, Proyect_2, Proyect_3
All of the tables have the same column names, the only difference between the tables is the table name. It starts with the prefix 'proyect'. The issue is that the program doesn´t know how many 'proyect' tables there are, so i can´t make a list of them and select data like always
I need something like this:
SELECT mydata FROM TABLES LIKE 'Proyect_%';
Any ideas? Thanks!
To get all tables with a common prefix
SHOW TABLES LIKE 'Proyect_%';
This will return rows of tables that matched the prefix. Example:
Proyect_1
Proyect_2
Proyect_3
In PHP you can create a UNION query that will pick up the tables returned by the above query,
$sql = "SHOW TABLES LIKE 'Proyect_%'";
$result = $conn->query($sql);
$dataQuery = array();
$query = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_array(MYSQLI_NUM)) {
$dataQuery[] = "SELECT * FROM {$row[0]}";
}
$query = implode(' UNION ', $dataQuery);
}
echo $query;
if you want to search for all tables with name like Proyect then you can get from MySQL information schema.
SELECT * FROM information_schema.tables
From here you can find table by table name
Is it possible to join 2 tables from different database?
Here is my query
public function getschedule($section){
$this->dbsections->select('*');
$this->dbsections->from($section);
//I want to join the column "teacher" of the "section_name" table that is in the "dbsections" database
//to the "id" column of the "teachers" table in the "dbusers" database
$this->dbsections->join('teachers', 'teachers.ID = '.$section.'.TEACHER');
$query = $this->dbsections->get();
$query = $this->dbsections->get();
return $query->result_array();
}
This code gives me error, obviously. I also tried
$this->dbsections->join('dbusers.teachers', 'teachers.ID = '.$section.'.TEACHER');
and
$this->dbsections->join('dbusers.teachers', 'teachers.ID = dbsections.'.$section.'.TEACHER');
But both gives me error
Error Number: 1096
No tables used
SELECT *
You need table name in select * as
$this->dbsections->select("$section.*");// write tour table name before *
And Remove one time
// $query = $this->dbsections->get();
Hello ,
$sql = "select col1 , col2 from table where id=2"; // sometimes query larger
$q = $conn->prepare($sql);
$q->execute(array_values($v));
$q->setFetchMode(PDO::FETCH_BOTH);
while($r = $q->fetch())
{
echo " $r[$i]";
}
Code is Working Fine.
Now i want To save Query Result to Another Temporary Table.
i Dont know no. of columns generated in Query result. Each time Query is different so columns and data is different. So How store that Query result to another table.
You could let MySQL do that work for you, e.g. via
CREATE TEMPORARY TABLE new_tbl SELECT * FROM orig_tbl WHERE ...
see http://dev.mysql.com/doc/refman/5.1/en/create-table.html
I have 2 databases on the same server with 2 identical tables.
What I want to do is select all records from both tables and join them in one array.
I've been messing around with the script below. For some reason it returns the records of db2.tbl twice and doesn't return the db1.tbl records at all. When I try to select the data from a single database is works fine for both of them. Does any one see the problem?
<?PHP
require_once("config.php");
$conn = #mysql_connect($dbhost, $dbuser, $dbpass)or die ('Error connecting to mysql server'.mysql_error());
$q = mysql_query("SELECT * FROM db1.tbl JOIN db2.tbl");
var_dump(mysql_num_rows($q));
while($arr = mysql_fetch_assoc($q)){
var_dump($arr);
}
?>
Is this what you want ? All records from database1 followed by all records from database2:
$q = mysql_query("SELECT * FROM db1.tbl UNION SELECT * FROM db2.tbl");
I assume the user you are connecting with has access to both databases.
Your query should work. However add tilt(`) to your database name table name. Execute the query first in mysql see whether it is ok than execute with php.
How can I using KohanaPHP framework and database module get mysql table structure?
I've tried this:
$query = DB::query(NULL, 'DESCRIBE table_name');
$result = $query->execute();
But it only returns number of columns in table, and foreach loop failed.
Is there any other way to get table structure or how can I update code above to works properly?
Try this:
$query = DB::query(NULL, 'SHOW FULL COLUMNS FROM table_name');
$result = $query->execute();
EDIT
You need to specify the type of query of DB::query() will just return the number of affected rows.
$query = DB::query(Database::SELECT, 'SHOW FULL COLUMNS FROM table_name');
$result = $query->execute();
This will give you the result you expect.