Delete rows across multiple databases - php

I'm using PHP and MySQL. I need to do a query:
DELETE FROM db1.players WHERE acc NOT IN (SELECT id FROM db2.accounts)
The problem is, that db1 and db2 are located on different servers. What is the fastest solution for such problem?
To be precise: I'm using 2 connections, so I think I can't use one query for it.

You will either have to save the list you want to an array and compare it with the other database
or
You can make a federated table and make it seem like the query is running on 1 database.

I don't know if it is the fastest, but I would create a temporary table on db2 containing account ids, export that table to db1 and run the query there.
For the exporting part you can either use mysql builtin export/import functions, where you would have to consider finding an unused table name or use the CSV-export/import of mysql.
If the expected number of results from the inner query is reasonably small, you can transfer from within PHP using strings:
$ids = query($db1, "SELECT GROUP_CONCAT(id) FROM accounts");
query($db2, "DELETE FROM players WHERE acc NOT IN ($ids)");

The syntax database.table.field should actually work in MySQL.
But I guess you've to run the query directly on the MySQL server. Not sure about this.
Have you tried the following?
DELETE FROM db1.players WHERE db1.players.acc NOT IN (SELECT db2.accounts.id FROM db2.accounts)
Or did I get you wrong and the two databases are not on the same server?
Have a look here: http://www.dottedidesign.com/node/14

I'd try something like:
create temporary table for the ids on the local server
statement to fetch ids from remote server
prepared statement to insert ids into temp. table
transfering ids remote->local
DELETE using the temp.table
<?php
$pdoRemote = new PDO('mysql:host=somewhere.else;dbname=foo', '...', '...');
$pdoRemote->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdoLocal = new PDO('mysql:host=localhost;dbname=bar', '...', '...');
$pdoLocal->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdoLocal->exec('CREATE TEMPORARY TABLE remoteIds ( id int )'); // maybe engine=heap
$stmtLocal = $pdoLocal->prepare('INSERT INTO remoteIds (id) VALUES (:id)');
$stmtLocal->bindParam(':id', $id);
foreach( $pdoRemote->query('SELECT id FROM accounts') as $row ) {
$id = $row['id'];
$stmtLocal->execute();
}
unset($pdoRemote);

To query multiple databases, on different servers, using a single query you can use federated tables.
You could also use replication, or temporary tables. This way you're accessing the databases on the same server.
The only other alternative I can think of would be to use MySQL Proxy to parse your query into 2 or more queries.
I think I'm right in saying that you can't do it the way you are be able to with MSSQL using the sp_addlinkedserver procedure.
You might be better using two queries.

Related

Is possible to join two tables (One Mysql, the other Oracle) from two different servers in PHP?

I'll spare the back story, but basically I have two different databases that I retrieve information from. 1 being Oracle (which we've only been given read-only access to) and the other being Mysql (which we have full access to). My problem is there is column in the oracle table that is missing that we would like to pull data from, however that column we need is in the mysql table. Is there a way that I can write a join statement to include this mysql column to the oracle table? I've seen instances online where people have used 'LinkedServers', but I wasn't sure how that worked working in PHP. The DB connections are stored in a php file and the function is called when we want to use that particular database.
tl;dr
Can a user use a join for a mysql and oracle table? if so, how can it be implemented in php when the db info is in a separate php file?
I imagine the sql query would look similar to this:
select
*
from
LocalTable,
[OtherServerName].[OtherDB].[dbo].[OtherTable]
but with some join logic
You cannot join 2 different database vendors.
A query is executed by a specific vendor query analyzer.
You can however do the join itself IN php, but would require to dump a lot of info into php. Should be okay for small data sets not okay for large ones...
What you would want to do is copy the data from one vendor into a temp table in the other then execute the join.
For the purpose of this example I will use PDO.
I am going to take the oracle data and put it into mysql so that I can use a join on that data...
$oracle_table_b = "select * from mytable";
$oracle_data = $pdo->fetchAll();
$stmt = $pdo_mysql->prepare("insert into mytemptable (mycols)");
foreach ($oracle_data as $row) {
$stmt->execute($row);
}
//Then run your join

How can tables in a mysql database be counted?

I'm using MySQL v.5.0.77 and I was wondering if it is possible to count the number of tables in a database. I have done a lot of research and am unable to find a way to do this that is not depreciated.
For each user that signs up I had generated a table in a database for them. And now I am trying to get a live count of those tables, if that makes sense. Is this a logical way of storing user information? I am unable to programmatically create entire databases on my server.
You can do a query to information_schema.tables
SELECT COUNT(*) FROM information_schema.TABLES
WHERE TABLE_SCHEMA='$database_name';
You could do it like this:
SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='YOUR_DB_NAME_HERE'
http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
For each user that signs up I had generated a table in a database for them. And now I am trying to get a live count of those tables, if that makes sense. Is this a logical way of storing user information?
If you're creating a separate table for each and every user then probably not. There are better ways to store the data and relationships, which can be more complicated to learn but will provide far more flexibility, abilities, and scalability later on.
I think you're probably trying to reproduce the functionality of the database in your programming e.g. getting a list of users would require you to run a query on every single user table.
I recommend you take a look at database design and database normalization (try to focus on the concept of how best to store data first without getting bogged down in the specifics).
I don't know if this is slower than ajreal's answer but I've been using :
$sql = "SHOW TABLES";
$res = mysql_query($sql);
$num_tables = mysql_num_rows($res);
Open Terminal, and type use myDB, then show tables;
It will gives you total tables name and last line as total tables count 47 rows in set (0.00 sec)
Or type below query
SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='myDB'

How to sync two database tables with each other in mysql PHP

I have two tables called clients, they are exactly the same but within two different db's. Now the master always needs to update with the secondary one. And all data should always be the same, the script runs once per day. What would be the best to accomplish this.
I had the following solution but I think maybe theres a better way to do this
$sql = "SELECT * FROM client";
$res = mysql_query($conn,$sql);
while($row = mysql_fetch_object($res)){
$sql = "SELECT count(*) FROM clients WHERE id={$row->id}";
$res1 = mysql_query($connSecond,$sql);
if(mysql_num_rows($res1) > 0){
//Update second table
}else{
//Insert into second table
}
}
and then I need a solution to delete all old data in second table thats not in master.
Any advise help would be appreaciated
This is by no means an answer to your php code, but you should take a look # Mysql Triggers, you should be able to create triggers (on updates / inserts / deletes) and have a trigger (like a stored proceedure) update your table.
Going off the description you give, I would create a trigger that would check for changes to the 2ndary table, then write that change to the primary table, and delete that initial entry (if so required) form the 2ndary table.
Triggers are run per conditions that you define.
Hopefully this gives you insight into 'another' way of doing this task.
More references on triggers for mysql:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
You can use mysql INSERT ... SELECT like this (but first truncate the target table):
TRUNCATE TABLE database2.client;
INSERT INTO database2.client SELECT * FROM database1.client;
It will be way faster than doing it by PHP.
And to your notice:
As long as the mysql user has been given the right permissions to all databases and tables where data is pulled from or pushed to, this will work. Though the mysql_select_db function selects one database, the mysql statement may reference another if you use complete reference like databasename.tablename
Not exactly answering your question, but how about just using 1 table, instead of 2? You could use a fedarated table to access the other (if it's on a different mysql instance) or reference the table directly (like shamittomar's suggestion)
If both are on the same MySQL instance, you could easily use a view:
CREATE VIEW database2.client SELECT * FROM database1.client;
And that's it! No synchronizing, no cron jobs, no voodoo :)

PHP-Mysql table join from different host

There is a table employee in the database abc_db at abc#localhost(server) and there is another table department in the database xyz_db at xyz#localhost(server). How can I join the tables using php mysql connection. I have written the following code but it does not generate any resource id.
$conn = mysql_connect("localhost","abc","abcdb");
$conn1 = mysql_connect("localhost","xyz","xyzdb");
$db_select=mysql_select_db("abc_db",$conn);
$db_select1=mysql_select_db("xyz_db",$conn1);
$sql="SELECT * FROM employee e LEFT JOIN department d ON e.department_id=d.id ";
$res=mysql_query($sql);
You can't join two tables using different connections to the database, not from PHP, nor on the MySQL server. (#RobertPitt has a good point: do you actually need two connections? It's possible to join two tables on the same host, but in different databases, within one connection - assuming your connection has the necessary privileges to access both)
If you have control over one or other of the databases, you might try setting up a federated table; make sure that the performance is OK though (if the db machines don't have a fast, low-latency connection (i.e. directly joined by a cable), don't bother), and there is a long list of limitations.
Possible lesser evils:
replicate the table from one server to the other (tricky to set up)
"join" them manually in PHP (gross, inefficient, but pretty much your only choice if you don't have control over the database)
You can make select between different databases and tables if used user has permissions to all of them (this is not possible if database hosts are different).
This is just an example:
SELECT `table_a`.`column` AS `table_a_column`, `table_b`.`column` AS `table_b_column`
FROM `database_a`.`table` AS `table_a`
JOIN `database_b`.`table` AS `table_b` ON `table_b`.`smth` = `table_a`.`smth_else`
Its not possible, the only way its possible on PHP would be when your using clusters / several nodes with a replication setup.
Obviously you don't understand some of the more technical stuff in regards to mysql, but give this ago.
$master = mysql_connect("master_host","abc","abcdb");
$slave = mysql_connect("slave_host","xyz","xyzdb");
if(mysql_select_db("abc_db",$master) && mysql_select_db("xyz_db",$slave))
{
//Both Selected
$sql = mysql_query("SELECT * FROM employee",$master);
$rows = array();
while($row = mysql_fetch_assoc($sql))
{
//Query the other $slave DB here!
$slave_sql = mysql_query("SELECT * FROM department WHERE department_id = " . $row['id'],$slave);
while($sub_row = mysql_fetch_assoc($slave_sql))
{
$row = array_combine($sub_row,$row);
}
$rows[] = $row;
}
}
But thats not what you want to be doing really.
Just incase your trying to join across 2 databases on the same server, as both your hosts in your code are locahost, this is possible
SELECT * FROM db1.employees db1_e,db2.records db2_r WHERE db1_e.employee_id = db2_r.record_eid
But not sever 1 to an external server without using replication, even then you can replication wont be much help.
References and links:
http://nathan.rambeck.org/blog/2-joining-mysql-tables-across-multiple-databases
http://dev.mysql.com/doc/refman/5.0/en/replication.html
You can't join them "on-the-fly" using neither PHP nor SQL, what you can do is fetch data from both hosts and compare/join them manually using PHP.
Simply we can say. You can't run or join two tables of two different database in PHP.
you can run query on mysql prompt but not in .php file because every time you need to call database connection in mysql_query(); so you can't run two database at same time.
you have single choice just take first database's table's data in array and then run array loop or you can use it's id with IN in mysql or run two while loop.

Multiple database in single query is possible?

[PHP] How can I query a data from two database in one statement?
Please give me the easy way. and How to connect 2 database to use it?
Thank you
It is possible to use database tables from different databases in one query, if your current connection is allowed to access both databases.
You just need to prefix every table name with the database name:
SELECT * FROM `databasename`.`tablename` ...
... LEFT JOIN `databasename_2`.`tablename`....
A 'database' in MySQL terms is a logical unit within a database server. To query tables from two separate databases, see Pekka's answer (though pleas note that restrictions apply - some JOINS might not work as intended etc. For more info see the MySQL docs.)
If you want to query two different database servers within the same statement, then the answer is that that isn't possible. You will have to create two separate connections, and query each of them individually.

Categories