select all rows from multiple db files - php sqlite3 - php

I have multiple db files which contains the same table with different values.
example files:
wifi_16-09-02_09_34_03.db
wifi_16-09-02_09_44_06.db
wifi_16-09-02_09_60_02.db
How can I select all the rows from multiple files?
I only know to do this with one file.
Here is my code:
$dbfile = 'wifi_16-09-02_09_34_03.db';
$db = new SQLite3('dbs/'.$dbfile);
$sql = 'SELECT * FROM wifi';
$results = $db->query($sql);
I am new on SQLite3, any help would be appreciated.

You can simply open the other databases in the same way.
If you want to have all rows in a single query, you need to attach all the other databases to some main database, and use a compound query:
$db->exec("ATTACH 'dbs/wifi_16-09-02_09_44_06.db' AS db2");
$db->exec("ATTACH 'dbs/wifi_16-09-02_09_60_02.db' AS db3");
...
$sql = <<<'SQL'
SELECT * FROM main.wifi
UNION ALL
SELECT * FROM db2.wifi
UNION ALL
SELECT * FROM db3.wifi
SQL;
$results = $db->query($sql);

Related

Select Data From Multiple MySQL Tables

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

Why is this Union Query not working

I am trying to access multiple tables from mySQL via my php in wordpress. My code looks like this...
function retrieve_libraries( $data ) {
$second_db = new wpdb(DB_USER, DB_PASSWORD, "saic3_LibraryIndex", DB_HOST);
$query = "SELECT * FROM `Library` WHERE library_id=74 UNION SELECT * FROM `Library_Collection` WHERE library_id=74";
$list = $second_db->get_results($query);
return $list;
}
Before I had my query like this...
$query = "SELECT * FROM `Library` WHERE library_id=74"
And the data returned just fine. I have been looking into the UNION statement and the UNION ALL and both seem to not work. Am I calling this wrong? is my syntax off? Asking for a friend.
UNION isn't going to work unless Library and Library_Collection have the same number and type of columns.
Based on the table names*, it looks like you probably want to JOIN the two tables instead of UNION them.
$query = "SELECT * FROM `Library`
LEFT JOIN `Library_Collection`
ON Library.library_id = Library_Collection.library_id
WHERE Library.library_id=74";
*(I'm assuming Library_Collection is the things a Library has.)

Select all data from a table and insert it to another table

I wanted to select all the data from one of my table and insert it to another table. I have this code but it wasn't working..This is what the phpMyadmin says "MySQL returned an empty result set (i.e. zero rows). (Query took 0.0010 seconds.)"
Here is my code:
if(isset($_POST['select_table']))
{
$select_table = mysql_real_escape_string($_POST['select_table']);
$query_select = "INSERT INTO pdf_table
SELECT * FROM $select_table";
$select_query = mysql_query($query_select,$connectDatabase);
}
Please ensure that both the tables have equal number of columns.
It is a good practice to use following way for inserting records with select query :-
INSERT INTO pdf_table (column_1, column_2) SELECT column_1, column_2 FROM $select_table
your sql query is like
INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;
The approach you are using INSERT INTO pdf_table SELECT * FROM $select_table will give you only single row inserted from 1st table to another table.
As per your requirement you want all the records from 1st table should get insert in your pdf_table
So i will like to suggest try the following approach
May be this is what you looking for
if(isset($_POST['select_table']))
{
$select_table = mysql_real_escape_string($_POST['select_table']);
$select_query = mysql_query( "select * from $select_table ",$connectDatabase);
while ($row = mysql_fetch_array($select_query))
{
mysql_query( "insert into pdf_table values($row['column1'],$row['column2'],...,$row['columns']) ",$connectDatabase);
}
}
try this one :
if(isset($_POST['select_table']))
{
$select_table = mysql_real_escape_string($_POST['select_table']);
$query_select = " Create Table pdf_table ( SELECT * FROM $select_table); ";
$select_query = mysql_query($query_select,$connectDatabase);
}
but, this query will create new table "pdf_table".

how to get multiple result from multiple queries

I have this query
$query = "Select * FROM table WHERE table.firs_column = 1;
Select * FROM table WHERE table.second_column = 1;
Select * FROM table WHERE table.third_column = 1;
Select * FROM table WHERE table.column = 1";
$stmt = $db->prepare($query);
$result = $stmt->execute();
I want to have multiple results, each one have the result of one query!
how to do it?
It looks like you are using PDO, so you could do something like:
$first_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$second_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$third_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$fourth_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
To get your 4 rowsets.
You can use UNION if table is same for your multiple queries
Select * FROM table WHERE table.firs_column = 1
UNION
Select * FROM table WHERE table.second_column = 1
UNION
Select * FROM table WHERE table.third_column = 1
UNION
Select * FROM table WHERE table.column = 1
After you finish with the first result set, use nextRowset() method to advance to the next result set.
You need to have a PHP MySQL driver extension that supports this method.
Not sure what you're aiming at, but did you try UNION?
Your SQL statement would look like this:
SELECT * FROM table WHERE table.firs_column = 1
UNION
SELECT * FROM table WHERE table.second_column = 1
UNION
SELECT * FROM table WHERE table.third_column = 1
UNION
SELECT * FROM table WHERE table.column = 1;
Please show your desired result if you think of something different.
Since we don't know anything about your database structure, I suggest looking into mysqli::multi_query().
If you're trying to pull related data, I highly suggest you look into doing MySQL JOINs instead. MySQL is another language unto itself that should be learned as a distinct language rather than just a string to be contatenated in PHP.

PHP how to connect more then one db

how to connect more then one DB in php... but the DB servers are same . but the DB is different. same single page i need to fetch the result from all the 3 db to display. Thank you
Easy: make multiple connections. Each connection returns a resource handle which you assign to a variable. So you just put each connection into it's own variable.
Method 1:
Don't select databases; put the database name before the table:
mysql_connect('localhost','db_user','pssword');
mysql_query('SELECT * FROM database_1.table_name');
Method 2:
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
$handle_db3 = mysql_connect("localhost:3306","myuser","apasswd");
$handle_db4 = mysql_connect("localhost","otheruser","apasswd");
mysql_select_db("db1",$handle_db1);
mysql_select_db("db2",$handle_db2);
mysql_select_db("db3",$handle_db3);
mysql_select_db("db4",$handle_db4);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
mysql_query($query,$which);
Just building more database handles,that's just fine.
http://php.net/mysql_connect, note the parameters
also, if all these DBs share same server, you can just specify particular db using . syntax:
SELECT * FROM db1.table ...
SELECT * FROM db2.table ...
SELECT * FROM db3.table ...

Categories