简体   繁体   中英

Send database table from one server to another database table in different server PHP MySQL

I am trying to send database table from one server to another database table in different server. Here is the PHP code I have tried so far, but to no avail:

<?php
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 1
mysql_select_db('db1_name',$dblink1);  // select database 1

$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 2   
mysql_select_db('db2_name',$dblink2); // select database 2

$table='output_table';#not sure if this is correct

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

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

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

mysql_close($dblink1); 
mysql_close($dblink2);
echo "complete";
?>

I have tried the output_table with and without the same column names/amount as first_table, but it does not seem to be working.

Again, any help would be appreciated.

The problem might be that $dblink2 is undefined.

$dblink2=mysql_connect('server', 'user', 'pass'); // connect server 2   
mysql_select_db('db2_name',$dblink2); // select database 2

Also consider using pdo methods instead because mysql- methods are now less favored to the PDO way of working with database objects.

This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.

You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.

o to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used

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