2

I would like to make a page for uploading some of my local records to the web server.

The Table Structure from local and web server are the same.

My question is, how to connect to both server?

<?php
$localCon = mysql_connect("localhost", "root", "");
if (!$localCon) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("[table]", $localCon);

$serverCon = mysql_connect("mysql", "[username]", "[password]");
if (!$serverCon) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("[table]", $serverCon);


$result = mysql_query("SELECT * FROM [table]");

while ($row = mysql_fetch_array($result)) {
    echo $row['[coloumn]'];
    echo "<br />";
}

mysql_close($localCon);
mysql_close($serverCon);
?>

I got error for both local and web server.

Or is there another way for me to upload the local records to the web?

This is the error msg:
Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in [addres]
Warning: mysql_connect() [function.mysql-connect]: [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (trying to connect via tcp://mysql:3306) in [addres]
Warning: mysql_connect() [function.mysql-connect]: php_network_getaddresses: getaddrinfo failed: No such host is known. in [address]
Could not connect:
Sieryuu
  • 1,510
  • 2
  • 16
  • 41

2 Answers2

2

You can connect to more databases but you need the handle in your query.

$result = mysql_query("SELECT * FROM [table]", $serverCon);

Otherwise you don't know from where you make the query and its not working.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • But anyway AFAIK, connecting local server is when we put the file in the local, and connecting web server is when we put the file on the web, can we connect both, if I just wan to put it in the local place? – Sieryuu Nov 27 '12 at 01:14
  • Yes but you should look at the performance. A connection to a remote host has no good performance. So if you need only a replication you could use a Master/Slace replication. – René Höhle Nov 27 '12 at 08:46
1

The error is clear: Unless you have an entry for mysql in your hosts file, mysql is not a valid network address.

You need to specify the address of the remote server as the first parameter in your mysql_connect call.

Please note that the mysql_* functions are deprecated and it is recommended to switch to PDO or mysqli with prepared statements.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Yeah, but is there any possibilities to `upload` my local data to the web server?, AFAIK, connecting web server need to put my `PHP` file in the server, but if I put in the server, I cannot connect my local `database` – Sieryuu Nov 27 '12 at 01:26
  • @Sieryuu Don't know about the `mysql_*` extension, but I don't see any reason why I could not connect to multiple databases / servers in PDO. – jeroen Nov 27 '12 at 01:29
  • Can PDO connect local and server at the same time? – Sieryuu Nov 27 '12 at 01:49