将数据库复制到服务器上的另一个数

时间:2015-01-21 09:29:11

标签: php mysql database

我有问题。我不是很擅长这个。 :) 让我们说这是数据库anna_subscribers,我想每次将这个.php文件调用到DB anna_newsletter时复制这个数据库。 如何编辑此代码,以便它不会下载文件,而是将数据库复制到此服务器上的另一个数据库? 这是执行下载的此文件的代码:

<?
require_once('../../../../../initialize.php');
if(!$session->isLogedIn()) {
exit;
}
ini_set('memory_limit','16M');

$q=$db->get($db->query('SELECT sqlUrl, sqlDatabase, sqlUserName, sqlPassword, ID, importShopID, lastOrderUpdate FROM com_som_shops WHERE ID="3"'));
$dbhandle = new Database(false);
$dbhandle->open_connection(''.$q['sqlUrl'].'', ''.$crypt->decrypt($q['sqlUserName']).'', ''.$crypt->decrypt($q['sqlPassword']).'', ''.$crypt->decrypt($q['sqlDatabase']).'');   
$dbhandle->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");

$db_un_chQ = $dbhandle->query("SELECT * FROM newsletter_subscriber WHERE subscriber_status='1' AND store_id='3' AND customer_id='0'");
$string="";

while($db_un_chR=$dbhandle->fetch($db_un_chQ)) {
    $string.= $db_un_chR["subscriber_email"].",\n";
}

$dbhandle->close_connection();

header("Content-Type: text/plain; charset=utf-8");
header('Content-Disposition: attachment; filename="exportSubscrtiptionKondomanija_'.date("d.m.Y-H.i").'.txt"');

echo $string;
?>

我是否用这样的东西改变了ti:

INSERT INTO anna_newsletter..subscribers
SELECT * FROM anna_subscribers..subscribers

数据库结构:anna_subscribers有:

`ID``email``date_added`

数据库结构:anna_newsletter有:

`id``userID``email``custom_fields``list``unsubscribed``bounced``bounce_soft``complaint``l‌​ast_campaing``last_ares``timestamp``join_date``confirmed``messageID`

2 个答案:

答案 0 :(得分:0)

只需在查询中指定数据库:

 INSERT INTO DESINATION_DATABASE.subscribers SELECT * FROM SOURCE_DATABASE.subscribers 

答案 1 :(得分:0)

你可以这样:

<?php

$dblink1=mysql_connect('$ip1', '$user1', '$pass1'); // connect server 1
mysql_select_db('$database1',$dblink1);  // select database 1

$dblink1=mysql_connect('$ip2', '$user2', '$pass2'); // connect server 2 
mysql_select_db('$database2',$dblink2); // select database 2

$table='tabletest';

$tableinfo = mysql_fetch_array(mysql_query("SHOW CREATE TABLE $table  ",$dblink1)); // get structure from table on server 1

mysql_query(" $tableinfo[1] ",$dblink2); // use found structure to make table on server 2

$result = mysql_query("SELECT * FROM $table  ",$dblink1); // select all content     
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) {       
       mysql_query("INSERT INTO $table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')",$dblink2); // insert one row into new table
}

 mysql_close($dblink1);
 mysql_close($dblink2);

 ?>