从另一个更新一个mysql数据库

时间:2014-07-21 18:18:25

标签: php mysql

我在MySQL的这一部分中有点像新手。我认为我在查询的正确路径上并连接到两个数据库,但我有点不清楚如何在两个数据库上执行查询。有人能把我送到正确的方向吗?

这是我到目前为止所做的:

<?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";
?>

1 个答案:

答案 0 :(得分:0)

$dbh1$dbh2 MySQL链接标识符而非数据库名称,您应该在查询中替换它们:

<?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";
?>

如果两个DB位于同一服务器上,您也不必使用两个连接