PHP PDO LEFTJOIN on other database [duplicate] - php

This question already has answers here:
Join between tables in two different databases?
(4 answers)
Closed 6 years ago.
I have a project where I have two MySQL databases. They are completly separated. They are on two different servers. There is no way to use just one.
Is it possible to connect them both together that I could use e.g. LEFTJOIN?
Or do I have to split it in two separate queries and compare them by myself?

As long as a db user used to connect to first database has the access to the second, you can do cross-db joins, you just need to specify them:
SELECT db1.table.field, db2.othertable.otherfield
FROM db1.table
JOIN db2.othertable ...

Related

PHP script to copy mysql column to another table [duplicate]

This question already has answers here:
php and mysql copy record from one table to another
(4 answers)
Closed 3 years ago.
Is there any easy way to copy one a column in a table to another table without changing the order ?
I am a beginner here. I would be much thankful to you if you could supply in depth answer.
I think this query can help you out
INSERT INTO second_table SELECT * FROM first_table WHERE id IN(1,2,3,4...n)

MySQL Show Databases With Where Condition [duplicate]

This question already has answers here:
"Show databases" with condition
(4 answers)
Closed 5 years ago.
Is there a way to show a list of databases by condition?
Show databases WHERE table_name ='?' AND column_name = '?';
BEST BET:
Just itterate through all the databases, but if your trying to find a specific table why not just do this SHOW TABLES LIKE 'my_table_numer_%'; That will select a table with a begging part followed by a wild card text.

php PDO transfer resultset to database [duplicate]

This question already has answers here:
Automated or regular backup of mysql data
(2 answers)
Closed 6 years ago.
I try to transfer a huge amount of data (around 200k records every few minutes) from one to another database (also on two different servers). The tableschema on both tables is both dbs is the same.
So whats the best way to transfer a huge resultset into a db without causing an memory limit error.
my current solution looks like this. But this means I run about 200k Insert querys in writeToDB2() and that seems to be not very effective to me.
$stmt = $this->db_1->query("SELECT foo from bar");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
writeToDB2($row);
}
Does anyone knew a better solution to bulk transfer the data?
The other answer works only if the same user has access to both databases.
A general purpose solution is to forget about PHP and PDO at all and use console mysql shell and mysqldump like
mysqldump -uuser1 -ppassword1 db1 tablename | mysql -uuser2 -ppassword2 db2
Fortunately mysql supports INSERT SELECT spanning databases.
$stmt = $this->db_1->query("INSERT INTO db2.bar(foo) SELECT foo from db1.bar");
$stmt->execute();

Can you join 2 databases in mysql using php? [duplicate]

This question already has answers here:
PHP Mysql joins across databases
(4 answers)
Closed 9 years ago.
Is it possible to join 2 different databases in mysql using php?
for instance:
$sql = "SELECT * FROM db1.table LEFT JOIN db2.table USING (id)";
$result = mysql_query($sql);
--
I know how to create multiple new different database connections using php. But I'm unable to figure out if it's possible to acutally join two different databases in mysql using php in one query.
I believe you can do it simply like:
SELECT DB1Table.columnIWant, DB2Table.ColumnIWant
FROM DB1.AppropiateTable as DB1Table
JOIN DB2.TableToJoin as DB2Table
ON DB1Table.ID = DB2Table.ID
Assuming the user is auth'ed for both tables. Also - see this link on multiple connections:
How do you connect to multiple MySQL databases on a single webpage?

Retrieve articles from array of category ids [duplicate]

This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Use an array in a mysqli prepared statement: `WHERE .. IN(..)` query [duplicate]
(8 answers)
Closed 11 months ago.
I have an array of category ids and want to retrieve articles from my mysql database with these category ids. What is the best method for this?
mysql_query('SELECT * FROM articles WHERE category_id IN (\''.implode('\'',array_map('mysql_real_escape_string',$categories)).'\')');
Specify how articles are joined to categories if this is not how your db/table setup.
Look here:
I have an array of integers, how do I use each one in a mysql query (in php)?
for a safe, parameter-based approach and code sample.

Categories