Mysql更新表列从一个数据库到另一个数据库

时间:2014-07-18 18:23:33

标签: php mysql

我正在处理一个更新的脚本:

tendesig_zink_production | euid0_hikashop_product | product_quantity

为:

tendesig_zink_dev | euid0_hikashop_product | product_quantity

我必须这样做,因为我们的库存号存储在生产实例上。因此,在从我们的开发实例推送新版本之前,我必须在推送之前更新开发人员的生产库存。我的mySQL非常生疏,这是我到目前为止我需要知道正确的查询是什么。

<?php
$host1="localhost"; // destination
$base1="tendesig_zink_dev";
$user1="tendesig_zink";
$password1="1,&#GZAWiB5z";

$host2="localhost"; //source
$base2="tendesig_zink_production";
$user2="tendesig_zink";
$password2="1,&#GZAWiB5z";


$conection1 = @mysql_connect($host1, $user1, $password1) 
or die("Error reaching destination<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 1! <br>";

$Db1 = @mysql_select_db($base1, $conection1)
or die("Error reaching destination database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database1 reached!<br>"; 

$conection2 = @mysql_connect($host2, $user2, $password2) 
or die("Error reaching source<br>".mysql_error()." nr eroare: ".mysql_errno());
print "Succesfuly connected 2!<br>";

$Db2 = @mysql_select_db($base2, $conection2)
or die("Error reaching source database:<br>" . mysql_error(). "<br>" . mysql_errno());
print "Database2 reached!!<br>"; 



$query = 'create table destination_table select * from second_database.source_table'; 
//echo "<br>".$query."<br>"; 
$result2 = mysql_query($query2, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());



mysql_close($conection1);
mysql_close($conection2);
?>

1 个答案:

答案 0 :(得分:0)

您的查询错误。你想做这样的事情:

CREATE TABLE destination_database.destination_table LIKE source_database.source_table

然后运行:

INSERT INTO destination_database.destination_table 
SELECT * FROM source_database.source_table

(未经测试的)PHP代码:

$create_query = 'CREATE TABLE destination_database.destination_table LIKE source_database.source_table'; 
$result = mysql_query($create_query, $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());

$insert_query = 'INSERT INTO destination_database.destination_table SELECT * FROM source_database.source_table'; 
$result = mysql_query($insert_query , $conection1) or die('Query failed: ' . mysql_error().'||'.mysql_errno());