简体   繁体   中英

Update one mysql database from another

I'm a little of a rookie in the MySQL part of this. I think i am on the right path with the query and connecting to the two databses, but Im a bit unclear on how to execute the query over two databases. Can someone send me in the right direction?

Here is what I have so far:

<?php
    $dbh1 = mysql_connect('localhost', 'tendesig', 'password') or die("Unable to connect to MySQL");
    $dbh2 = mysql_connect('localhost', 'tendesig', 'password', true) or die("Unable to connect to MySQL");

    mysql_select_db('tendesig_dev', $dbh1);
    mysql_select_db('tendesig_production', $dbh2);

    $query = "UPDATE 
                tendesig_dev.euid0_hikashop_product, 
                tendesig_production.euid0_hikashop_product 
            SET 
                tendesig_dev.euid0_hikashop_product.product_quantity = tendesig_production.euid0_hikashop_product.product_quantity
            WHERE 
                'tendesig_dev.euid0_hikashop_product.product_id = tendesig_production.euid0_hikashop_product.product_id";
?>

$dbh1 and $dbh2 are MySQL link identifiers not DB names , you should replace them in the query:

<?php
    $dbh1 = mysql_connect('localhost', 'tendesig', 'password') or die("Unable to connect to MySQL");
    $dbh2 = mysql_connect('localhost', 'tendesig', 'password', true) or die("Unable to connect to MySQL");

    mysql_select_db('tendesig_dev', $dbh1);
    mysql_select_db('tendesig_production', $dbh2);

    $query = "UPDATE 
                tendesig_dev.euid0_hikashop_product, 
                tendesig_production.euid0_hikashop_product 
            SET 
                tendesig_dev.euid0_hikashop_product.product_quantity = tendesig_production.euid0_hikashop_product.product_quantity
            WHERE 
                tendesig_dev.euid0_hikashop_product.product_id = tendesig_production.euid0_hikashop_product.product_id";
?>

Also you don't have to use two connections if both DBs are on the same server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM